COMP4332/RMBI4310 Using NoSQL in Python Prepared by Raymond Wong NoSQL in Python Presented by Raymond Wong 1 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 NoSQL in Python 2 Including Package At the beginning of the “Python” script, include the following package. Python from pymongo import MongoClient NoSQL in Python 3 Database Connection Inside the “Python” script, include the following. Python Get the database connection client = MongoClient("mongodb://localhost:27017") … client.close() NoSQL in Python Perform some MongoDB operations Close the database connection 4 Database Connection For each Python operation related to MongoDB, it is better to include the following to handle exceptions related to MongoDB Python try: … Some operations in Python (including the previous operations just shown and the operations to be shown later) except pymongo.errors.ConnectionFailure as error: print(error) SQL 5 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) SQL in Python 6 Database Connection Alternatively, you could use “try… except” for each individual NoSQLrelated 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. SQL 7 Specifying Database Before we perform the MongoDB operation, we need to select the database as follows. Python Getting a database named “university” db = client["university"] … NoSQL in Python 8 MongoDB Operation 1. 2. 3. 4. 5. 6. Creating Collection Inserting Documents Updating Documents Querying Documents Removing Documents Dropping Collection NoSQL in Python 9 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. NoSQL in Python 10 MongoDB Operation 1. 2. 3. 4. 5. 6. Creating Collection Inserting Documents Updating Documents Querying Documents Removing Documents Dropping Collection NoSQL in Python 11 2. Inserting Documents Python Similar to what we have seen in MongoDB db.student.insert( { "sid": "12345678", "sname": "Raymond", "byear": 1998 } ) Remember to have the double-quotation on LHS (i.e., the field name) NoSQL in Python 12 MongoDB Operation 1. 2. 3. 4. 5. 6. Creating Collection Inserting Documents Updating Documents Querying Documents Removing Documents Dropping Collection NoSQL in Python 13 3. Updating Documents Python Similar to what we have seen in MongoDB 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”) NoSQL in Python 14 MongoDB Operation 1. 2. 3. 4. 5. 6. Creating Collection Inserting Documents Updating Documents Querying Documents Removing Documents Dropping Collection NoSQL in Python 15 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 “[…]” NoSQL in Python 16 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 “[…]” NoSQL in Python 17 4. Querying Documents (distinct) Similar to what we have seen in MongoDB Python listOfStudent = db.student.distinct("sname", {"byear":1998}) for oneStudent in listOfStudent: tempSname = oneStudent NoSQL in Python It returns a list of strings (for “sname”) 18 MongoDB Operation 1. 2. 3. 4. 5. 6. Creating Collection Inserting Documents Updating Documents Querying Documents Removing Documents Dropping Collection NoSQL in Python 19 5. Removing Documents Similar to what we have seen in MongoDB Python db.student.remove({"byear":1998}) NoSQL in Python 20 MongoDB Operation 1. 2. 3. 4. 5. 6. Creating Collection Inserting Documents Updating Documents Querying Documents Removing Documents Dropping Collection NoSQL in Python 21 6. Dropping Collection Similar to what we have seen in MongoDB Python db.student.drop() NoSQL in Python 22