Pymongo: Learn how to integrate mongoDB with Python - Complete CRUD operations for handling mongoDB data using python
MongoDb is one of most popular NoSQL database utilized nowadays. It is so because it is very simple to install and top use, beyond there are several cloud services on cloud which provides a mongoDb instance to use.
In the
other hand, python is a programming language typically adopted for big data
applications because of the simple way it handles with computer memory allowing
programs to have large numbers on memory using simple and native data types.
In this
article it will be presented how to connect in a mongoDB serve using python,
and how to perform all CRUD operations.
First it is
necessary to install the pymongo module, which can be easily installed with pip
tool, using the following instruction: python -m pip
install pymongo
Once it is
installed you my use the example code below, which imports the class
MongoClient, and when instantiate a object of this class, parametrize with the
connection string to create a link with mongo db server. Once it is done, you
may access a specific database and a specific collection within this database.
As you can see in the example below, the access of a database of a collection
is done accessing then as normal attributes within the client object.
# python -m pip install pymongo
from pymongo import MongoClient
import datetime
client = MongoClient('mongodb://localhost:27017/')
#Acessing a especific database
db = client.company
#Acessing a especific collection
collection = db.employees
Once we
have access to the collection object, this object that has methods to perform
the CRUD operations. The first operation we are going to see this the .find_one()
and .find(). The find_one, is quite simple to use. It basically returns the
first object of an collection, and the returned object can be accessed
directly. In the other hand, the find() method receives a parameter, which is
the “query object”. The “query object” is very important, because it will be
utilized in almost all methods. It is like the where clause in a SQL statement.
It is a normal JSON object, which each object attribute is a criteria the
document must match to return in the query. The find_all() method returns a
list, and then objects from the list needs to be individually accessed to have
access to its attributes.
print("### CRUD: Recovery")
print("### Find one ###")
document = collection.find_one()
print(document)
print("### Find ###")
resultSet = collection.find({'name': 'Rafael'})
for document in resultSet:
print(document)
print("### find all
###")
resultSet = collection.find({})
for document in resultSet:
print(document)
The next
method we are going to study is the insert_one. It is quite simple, basically
we have to pass a object as a method parameter, and it will be inserted int he
respective collection. It is simple, because as MongoDB is NoSQL, it is almost
error free operation (No matter the object structure, it will be inserted).
print("### CRUD: Create")
print("### .insert_one()
###")
document = {
"name": "Adrian",
"lastName": "Tepes"
}
print("Collection document count BEFORE insert:", collection.count_documents({}))
insert_result = collection.insert_one(document)
print("Collection document count AFTER insert:", collection.count_documents({}))
id = insert_result.inserted_id
print("Generated id on insert:", id)
Next, the
update or update_one method has two parameters: the query object, as we have
utilized in the find method, and also a update object, which includes the
attributes to be created or updated in objects that match the query object. It
is interesting to observe that if object already have a certain attribute it
will be overwritten, it it does not have it, it will be created.
print("### CRUD: Update")
print("### update: update_one or update_many ###")
query = {"_id": id}
newValues = {"$set": {"updated_on":
datetime.datetime.utcnow()}}
collection.update_one(query, newValues)
print("Updated object:", collection.find_one(query))
print("### update_many ###")
query = {"name": {"$ne": "Adrian"}} # != or
<> => different than operator
newValues = {"$set": {"updated_on":
datetime.datetime.utcnow()}}
collection.update_many(query, newValues)
resultSet = collection.find({})
for document in resultSet:
print(document)
The last
method we are going to see is the delete. The interface of this method is the
same of .find() method. The main difference is that instead of just returning
the objects that match the query, it will delete these objects from the
respective collection?
print("### CRUD: Delete")
print("### Delete: .delete_one or .delete_many ###")
query = {"name": "Adrian"}
print("Collection document count BEFORE delete:", collection.count_documents({}))
collection.delete_many(query)
print("Collection document count AFTER delete:", collection.count_documents({}))
resultSet = collection.find({})
for document in resultSet:
print(document)
So, do you
want to execute this code in your on computer? You can download that in the
link below?
This
explanation is too abstract? You may see a practical hands on demonstration in
this video?
Comments
Post a Comment