Python + MongoDB
Python + MongoDB
Python + MongoDB
T H E B E G I N N E R S G U I D E
PyMongo Driver
A R U N A R U N I S T O
MongoDB is a popular NoSQL (Not Only SQL)
database management system that provides
a flexible and scalable way to store and
manage data.
MongoDB is widely used in web
development, mobile app development, and
other scenarios where flexible and scalable
data storage is required. It's important to
choose the right database based on the
specific requirements of your application
and the nature of your data.
To connect Python to MongoDB, you can use
the official MongoDB drivers for python.
There are two official Python drivers:
1. PyMongo: For synchronous applications.
2. Motor: For asychronous applications.
For this part of the tutorial we are going to
use the PyMongo driver for synchronous
applications.
PyMongo is a Python driver for MongoDB
that provides a high-level API for interacting
with MongoDB databases.
Installing PyMongo:
You can install PyMongo using pip, the
Python package installer. Open your
terminal or command prompt and run:
Importing PyMongo:
We are going to import the MongoClient
class to create a connection from pymongo.
print("Databases:\n",client.list_database_names())
db = client.test
On the above line of code we select the
database named test.
After connecting to database we are going to
select the collection from the database here
I am going to select my collection named
test_collection.
test_collec = db.test_collection
sample_data = {
"name":"arun arunisto",
"age":27,
"tags":["developer", "tech geek"],
"details":{
"job":"full-stack developer",
"organisation":"nic"},
"created_at":datetime.datetime.utcnow()
}
# inserting one data using insert_one() function
result = test_collec.insert_one(sample_data)
sample_data_1 = {
"name":"arun",
"age":27,
"tags":["pythonista", "cyborg"],
"details":{
"job":"AI/ML",
"organisation":"xAI"
},
"created_at":datetime.datetime.utcnow()
}
sample_data_2 = {
"name":"arunisto",
"age":27,
"tags":["backend", "fast api"],
"details":{
"job":"IOT Engineer",
"organisation":"tesla"
},
"created_at":datetime.datetime.utcnow()
}
document_ids = result.inserted_ids
print("No. Documents added:",len(document_ids))
print("_ids of inserted:\n",document_ids)
Accessing Data:
Next, we are going to access data using
find_one() and find() methods. and also we
are going to import two packages for getting
the results in BSON format.
Importing Packages:
Updating data:
doc_to_update = {'_id':
ObjectId('654e5e80ad5db1b63c82220c')}
field_to_update = {"$inc":{"age":1}}
result = test_collec.update_one(
doc_to_update, field_to_update)
print("Updated Count:",result.modified_count)
# updating multiple document
# using update_many() function
doc_filter = {"age":{"$eq":27}}
add_new_field = {"$inc":{"age":1}}
result = test_collec.update_many(
doc_filter, add_new_field)
print("Document Matched:",result.matched_count)
print("Updated:",result.modified_count)
Deleting Data:
# deleting documents
# using delete_one() method
document_to_delete = {"name":"test1"}
#deleting using delete_one() method
result = test_collec.delete_one(
document_to_delete)
print("No. of deleted documents:",
result.deleted_count)
# deleting multiple elements
# using delete_many() method
doc_to_filter = {"age":{"$eq":28}}
result = test_collec.delete_many(
doc_to_filter)
print("No.of Documents deleted:",
result.deleted_count)
client.close()