1 / 24

COMP4332/RMBI4310

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.

medlock
Download Presentation

COMP4332/RMBI4310

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP4332/RMBI4310 Using NoSQL in Python Prepared by Raymond Wong Presented by Raymond Wong

  2. 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

  3. Including Package • At the beginning of the “Python” script, include the following package. Python from pymongo import MongoClient

  4. 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

  5. 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)

  6. 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)

  7. 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.

  8. 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"] …

  9. MongoDB Operation • Creating Collection • Inserting Documents • Updating Documents • Querying Documents • Removing Documents • Dropping Collection

  10. 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.

  11. MongoDB Operation • Creating Collection • Inserting Documents • Updating Documents • Querying Documents • Removing Documents • Dropping Collection

  12. 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)

  13. MongoDB Operation • Creating Collection • Inserting Documents • Updating Documents • Querying Documents • Removing Documents • Dropping Collection

  14. 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”)

  15. MongoDB Operation • Creating Collection • Inserting Documents • Updating Documents • Querying Documents • Removing Documents • Dropping Collection

  16. 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 “[…]”

  17. 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)

  18. 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 “[…]”

  19. 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

  20. 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)

  21. MongoDB Operation • Creating Collection • Inserting Documents • Updating Documents • Querying Documents • Removing Documents • Dropping Collection

  22. 5. Removing Documents Similar to what we have seen in MongoDB Python db.student.remove({"byear":1998})

  23. MongoDB Operation • Creating Collection • Inserting Documents • Updating Documents • Querying Documents • Removing Documents • Dropping Collection

  24. 6. Dropping Collection Similar to what we have seen in MongoDB Python db.student.drop()

More Related