240 likes | 252 Views
Learn how to use PyMongo in Python to perform MongoDB operations, including creating collections, inserting documents, updating documents, querying documents, removing documents, and dropping collections.
E N D
COMP4332/RMBI4310 Using NoSQL in Python Prepared by Raymond Wong Presented by Raymond Wong
PyMongo • We can use MongoDB in Python(when we are running the MongoDB server). • We should install the package “PyMongo” (i.e., “pymongo”) in Python
Including Package • At the beginning of the “Python” script, include the following package. Python from pymongo import MongoClient
Database Connection • Inside the “Python” script, include the following. Python Get the database connection client = MongoClient("mongodb://localhost:27017") … client.close() Perform some MongoDB operations Close the database connection
Database Connection • For each Python operation related to MongoDB, it is better to include the following to handle exceptions related to MongoDB Python try: … except pymongo.errors.ConnectionFailure as error: print(error) Some operations in Python (including the previous operations just shown and the operations to be shown later)
Database Connection • In summary, one simple implementation can be: Python try: client = MongoClient("mongodb://localhost:27017") … client.close() except pymongo.errors.ConnectionFailure as error: print(error)
Database Connection • Alternatively, you could use “try… except” for each individual NoSQL-related operation (rather than a whole block of all NoSQL-related operations) • In the following, we just focus on NoSQL-related operations. Thus, the operations handling exceptions will not be shown in the following slides.
Specifying Database • Before we perform the MongoDB operation, we need to select the database as follows. Getting a database named “university” Python db = client["university"] …
MongoDB Operation • Creating Collection • Inserting Documents • Updating Documents • Querying Documents • Removing Documents • Dropping Collection
1. Creating Collection • In MongoDB, there is no need to create a collection explicitly (e.g., db.createCollection("student")). • When we insert the first document into a collection, the collection will be created automatically.
MongoDB Operation • Creating Collection • Inserting Documents • Updating Documents • Querying Documents • Removing Documents • Dropping Collection
2. Inserting Documents Similar to what we have seen in MongoDB Python db.student.insert( { "sid": "12345678", "sname": "Raymond", "byear": 1998 } ) Remember to have the double-quotation on LHS (i.e., the field name)
MongoDB Operation • Creating Collection • Inserting Documents • Updating Documents • Querying Documents • Removing Documents • Dropping Collection
3. Updating Documents Similar to what we have seen in MongoDB Python db.student.update_many({"sid": "12345678"}, {"$set":{"byear":2008}}) Here, we use “update_many” instead of “update” Remember to have the double-quotation on each internal operation (e.g., “$set”)
MongoDB Operation • Creating Collection • Inserting Documents • Updating Documents • Querying Documents • Removing Documents • Dropping Collection
4. Querying Documents (find) Similar to what we have seen in MongoDB Python listOfStudent = db.student.find({"byear":1998}) for oneStudent in listOfStudent: tempSid = oneStudent["sid"] tempSname = oneStudent["sname"] tempByear = oneStudent["byear"] We could get the field value by specifying with “[…]”
4. Querying Documents (find) Python listOfStudent = db.student.find({"byear":1998}) recordNo = 0 for oneStudent in listOfStudent: recordNo = recordNo + 1 tempSid = oneStudent["sid"] tempSname = oneStudent["sname"] tempByear = oneStudent["byear"] print("Record {:d}: (sid={:s}, sname={:s}, byear={:d})".format( recordNo, tempSid, tempSname, tempByear) ) Output Record 1: (sid=12345678, sname=Raymond, byear=1998) Record 2: (sid=56785678, sname=David Lee, byear=1998) Record 3: (sid=88888888, sname=Test Test, byear=1998)
4. Querying Documents (aggregate) Similar to what we have seen in MongoDB Python listOfStudent = db.student.aggregate([{"$match": {"byear": 1998}}]) for oneStudent in listOfStudent: tempSid = oneStudent["sid"] tempSname = oneStudent["sname"] tempByear = oneStudent["byear"] We could get the field value by specifying with “[…]”
4. Querying Documents (distinct) Similar to what we have seen in MongoDB Python listOfStudent = db.student.distinct("sname", {"byear":1998}) It returns a list of strings (for “sname”) for oneStudent in listOfStudent: tempSname = oneStudent
4. Querying Documents (distinct) Python listOfStudent = db.student.distinct("sname", {"byear":1998}) recordNo = 0 for oneStudent in listOfStudent: recordNo = recordNo + 1 tempSname = oneStudent print(" Record {:d}: (sname={:s})".format(recordNo, oneStudent)) Output Record 1: (sname=Raymond) Record 2: (sname=David Lee) Record 3: (sname=Test Test)
MongoDB Operation • Creating Collection • Inserting Documents • Updating Documents • Querying Documents • Removing Documents • Dropping Collection
5. Removing Documents Similar to what we have seen in MongoDB Python db.student.remove({"byear":1998})
MongoDB Operation • Creating Collection • Inserting Documents • Updating Documents • Querying Documents • Removing Documents • Dropping Collection
6. Dropping Collection Similar to what we have seen in MongoDB Python db.student.drop()