HCI NoSQL Databases 2. PyMongo
Uploaded by adrianwang2003 · 28 May 2024
Preview
NoSQL Databases CPDD Computer Education Unit Version: Feb 2019 1 Name: __________________________ ( ) Class: _________ Date: _________ Lesson 2: Using PyMongo Instructional Objectives: By the end of this task, you should be able to: Use PyMongo to connect to MongoDB server Create MongoDB databases using PyMongo Access MongoDB databases using PyMongo Obtain and modify MongoDB documents with PyMongo Use query operators in PyMongo What is PyMongo? MongoDB databases can be accessed using different programming languages like C, Java and Python. To access MongoDB databases using Python, we use the Python driver for MongoDB, PyMongo. To use PyMongo, start your Python program by importing the pymongo package. Try typing and running program 1 below. (Remember to start the MongoDB server before you run the program.) The program connects to the MongoDB server and outputs the databases currently in the MongoDB server. Program 1: access.py 1 2 3 4 5 6 import pymongo client = pymongo.MongoClient("127.0.0.1", 27017) databases = client.database_names() print("The databases in the MongoDB server are:") print(databases) client.close() Line 2 connects to the local MongoDB database which is usually at port 27017. You can see the port number when you start the MongoDB server. Line 6 closes the connection to the server. The MongoDB server window should remain open while you want to access the MongoDB database.
NoSQL Databases CPDD Computer Education Unit Version: Feb 2019 2 Line 3 of the code retrieves the names of the databases, stored as a Python list. As an example, let’s create a database to store details on movie information. Please note that MongoDB waits until you have inserted at least one document before it actually creates the database and collection. Program 2: insert.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import pymongo client = pymongo.MongoClient("127.0.0.1", 27017) db = client.get_database("entertainment") coll = db.get_collection("movies") coll.insert_one({"_id":1, "title":"Johnny Maths", "genre":"comedy"}) coll.insert_one({"title":"Star Walls", "genre":"science fiction"}) coll.insert_one({"title":"Detection"}) #no genre list_to_add = [] list_to_add.append({"title":"Badman", "genre":"adventure", "year":2015}) list_to_add.append({"title":"Averages", "genre":["science fiction","adventure"], "year":2017}) list_to_add.append({"title":"Octopus Man", "genre":"adventure", "year":2017}) list_to_add.append({"title":"Fantastic Bees", "genre":"adventure", "year":2018}) list_to_add.append({"title":"Underground", "genre":"horror", "year":2014}) coll.insert_many(list_to_add) c = db.collection_names("entertainment") print ("Collections in entertainment database: ",c) client.close() Program 2 demonstrates two ways of inserting documents into collection entertainment. To insert one document, you
Content continues in the PDF.
Related notes
- VJC Chapter 21 SQLite with PythonNotes/Practices · 2025
- VJC Chapter 23 Web Applications PrinciplesNotes/Practices · 2025
- VJC Chapter 10 RecursionNotes/Practices · 2025
- VJC Chapter 20 SQLNotes/Practices · 2025
- VJC Chapter 16 Hash TableNotes/Practices · 2025
- VJC Chapter 22 NoSQLNotes/Practices · 2025

