Python MongoDB
MongoDB is a popular NoSQL database program which is used with Python to store and manage data. In this article, we will learn how to use Python to connect to MongoDB, perform operations like insert, update, delete and retrieve data from MongoDB.
Syntax
To use MongoDB with Python, we need to install the pymongo
module. The syntax for installing pymongo
module is as follows:
pip install pymongo
Example
Here is an example of using Python with MongoDB:
import pymongo
# Connect to MongoDB
client = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = client["mydatabase"]
mycol = mydb["customers"]
# Insert a record in the customers collection
mydict = { "name": "John", "address": "Highway 37" }
x = mycol.insert_one(mydict)
# Print the inserted record's id
print(x.inserted_id)
# Retrieve a record from the customers collection
x = mycol.find_one()
print(x)
# Update a record in the customers collection
myquery = { "address": "Highway 37" }
newvalues = { "$set": { "address": "Canyon 123" } }
mycol.update_one(myquery, newvalues)
# Delete a record from the customers collection
myquery = { "address": "Canyon 123" }
mycol.delete_one(myquery)
Output
The above example will produce the following output:
6085ffb013c8f9174fb4875d
{'_id': ObjectId('6085ffb013c8f9174fb4875d'), 'name': 'John', 'address': 'Highway 37'}
Explanation
In the above example, we first imported the pymongo
module. We then connected to MongoDB using the MongoClient
class, specifying the connection URL (mongodb://localhost:27017/
) and the name of our database (mydatabase
). Next, we accessed the customers
collection within our database using the mydb["customers"]
reference.
We then inserted a record into the customers
collection using the insert_one()
method. This method returns an object that contains the id of the inserted record, which we printed using print(x.inserted_id)
.
To retrieve a record from the customers
collection, we used the find_one()
method, which returns the first record that matches the specified query. We printed the retrieved record using print(x)
.
To update a record in the customers
collection, we used the update_one()
method, which takes two arguments: a query object and a new values object. The query object specifies which record(s) to update, and the new values object specifies the new values to set for the matched record(s). In our example, we updated the record with address "Highway 37" and set its address to "Canyon 123".
To delete a record from the customers
collection, we used the delete_one()
method, which takes a query object specifying which record(s) to delete. In our example, we deleted the record with address "Canyon 123".
Use
With Python and MongoDB, you can build powerful applications that can store and retrieve data quickly and easily. Some common use cases include:
- Building web applications that need to store user data, such as usernames, passwords, and preferences.
- Creating data-intensive applications that need to store large amounts of structured or semi-structured data, such as financial data or sensor data.
- Building real-time applications that need to store, modify, and retrieve data quickly.
Important Points
- MongoDB is a popular NoSQL database program that can be used with Python to store and manage data.
- To use MongoDB with Python, we need to install the
pymongo
module. - We can connect to MongoDB using the
MongoClient
class, specifying the connection URL and the name of our database. - We can perform operations like insert, update, delete, and retrieve data from MongoDB using methods like
insert_one()
,update_one()
,delete_one()
, andfind_one()
. - MongoDB is highly scalable and can handle large amounts of data, making it a popular choice for data-intensive applications.
Summary
In this article, we learned how to use Python with MongoDB to store and manage data. We covered the syntax for installing the pymongo
module, connecting to MongoDB, and performing operations like insert, update, delete, and retrieve data. We also discussed some common use cases for Python and MongoDB, and highlighted some important points to keep in mind when working with these technologies.