In this blog, we will connect MongoDB with Python with help of PyMongo. Then we will create a database, collection and documents. After that we will documents in Pandas's dataframe.
- Install Python and pip
- Install Jupyter notebook
- Install MongoDB community server
MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL).
PyMongo is a Python distribution containing tools for working with MongoDB, and is the recommended way to work with MongoDB from Python. The pymongo package is a native Python driver for MongoDB.
pip install pymongo
conda install -c anaconda pymongo
import pymongo
from pymongo import MongoClient
client = MongoClient()
client
client = MongoClient("mongodb://localhost:27017")
client
print(client.list_database_names())
I am creating a new database called "sampledb". You can give database name according to your choice.
db = client.sampledb
db
print(client.list_database_names())
If the database doesn’t exist, then MongoDB creates it for you, but only when we perform the first operation on the database.
student_collection = db["students"]
student_collection
MongoDB generates the ObjectId dynamically, so no need to add id.
student1 = { "name": "John", "age": 10, "class": "VI", "section": "A" }
result = student_collection.insert_one(student1)
result
print(f"Inserted new record id: {result.inserted_id}")
find(filter=None, projection=None, skip=0, limit=0, no_cursor_timeout=False, cursor_type=CursorType.NON_TAILABLE, sort=None, allow_partial_results=False, oplog_replay=False, batch_size=0, collation=None, hint=None, max_scan=None, max_time_ms=None, max=None, min=None, return_key=False, show_record_id=False, snapshot=False, comment=None, session=None, allow_disk_use=None)
Query the database.
The filter argument is a prototype document that all results must match.
for student in student_collection.find():
print(student)
Create the list of students.
students = [
{ "name": "Maria", "age": 9, "class": "VI", "section": "B"},
{ "name": "Michel", "age": 11, "class": "VII", "section": "A"},
{ "name": "Priyanka", "age": 8, "class": "IV", "section": "B"},
{ "name": "Jeena", "age": 12, "class": "X", "section": "A" }
]
result = student_collection.insert_many(students)
result
for student in student_collection.find():
print(student)
student = student_collection.find_one()
student
import pandas as pd
students = student_collection.find()
students
list_students = list(students)
list_students
df = pd.DataFrame(list_students)
df
students1 = student_collection.find({ "class": "VI" })
students1
list_students1 = list(students1)
list_students1
df1 = pd.DataFrame(list_students1)
df1