Examples to insert, update, and delete the MongoDB document in Python (Examples of Inserting, Updating, and Deleting Mongodb Documents in Python USINGO)
Examples to insert, update, and delete the mongoDB document in Python in Python
It is a very common task to use MongoDB in Python.PymonGo is an official driver that Python interacts with MongoDB.This article will provide you with examples of inserting, updating, and deleting MongoDB documents using Pymon.
1. Install and set mongodb
First, you need to install mongodb and set the relevant configuration.You can download the versions suitable for your operating system on the Mongodb official website (https://www.mongodb.com/try/download/Community), and install and set it in accordance with the guide.
2. Install the PyMongo library
You need to install the Pymono library before using PymonGo to perform MongoDB operation.You can use the following command to install pymongo in the terminal:
pip install pymongo
3. Import pymongo library
In Python, we need to import the Pymonogo library to use the function of MongoDB.Use the following code to import pymongo:
python
import pymongo
from pymongo import MongoClient
4. Connect MongoDB database
Before starting the MongoDB database, you need to connect to the database first.Use the following code to connect to mongodb:
python
# O
client = MongoClient('mongodb://localhost:27017/')
# To the specified database
db = client['mydatabase']
In the above example, we create a connection with MongoDB by specifying the connection URL (defaulting Localhost: 27017) of the MongoDB.Then, we choose the database we want to use, this is 'mydatabase'.
5. Insert document
The insert data is the process of adding the new document to the MongoDB collection.The following is an example code using pymongo inserting document:
python
# 选择 选择 选择
collection = db['mycollection']
# Insert a single document
document = {'name': 'John Doe', 'age': 30}
collection.insert_one(document)
# Insert multiple documents
documents = [
{'name': 'Alice Smith', 'age': 25},
{'name': 'Bob Johnson', 'age': 35}
]
collection.insert_many(documents)
In the example, we first choose to insert the collection of data.Then use the `Insert_one ()` function to add a separate document, or use the `Insert_many ()` function to add multiple documents.
6. Update documentation
Update MongoDB document allows us to modify the existing documents.The following is an example code to update the document with pymongo:
python
# 选择 选择 选择
collection = db['mycollection']
# Update a single document
collection.update_one(
{'name': 'John Doe'},
{'$set': {'age': 35}}
)
# Update multiple documents
collection.update_many(
{'age': {'$gt': 30}},
{'$inc': {'age': 1}}
)
In an example, we first choose a collection to be updated.Then, use the `update_one ()` function to update a single document that meets specific conditions, or use the `update_many ()` function to update multiple documents that meet specific conditions.
When updating the document, we need to specify the conditions to be updated and the fields to be updated.In an example, we use MongoDB's update operator `$ set` to update a field, and` $ Inc` to increase the value of a field.
7. Delete documentation
Deleting the MongoDB document is the process of removing documents that are no longer needed from the collection.The following is a sample code using pymongo to delete documentation:
python
# 选择 选择 选择
collection = db['mycollection']
# Delete a single document
collection.delete_one({'name': 'John Doe'})
# Delete multiple documents
collection.delete_many({'age': {'$gt': 30}})
In the example, we first choose to delete the collection of documents.Then, use the `Delete_one ()` function to delete a single document that meets specific conditions, or use the `delete_many ()` function to delete multiple documents that meet specific conditions.
When deleting documents, we need to specify the conditions to be deleted.In an example, we use MongoDB query operating symbols `$ gt` (greater than) to delete documents under specific conditions.
Summarize
This article provides examples of inserting, updating, and deleting MongoDB documents using PymonGo.Understanding how to use PyMongo for these operations can help you effectively interact with the MongoDB database in Python.
Before starting MongoDB, make sure that MongoDB has been installed and settled.Then, use the PyMongo library to connect to the database, select the collection, and use insertion, update and delete operations to operate the MongoDB document.