Docs Menu

Docs HomeDevelop ApplicationsPython DriversPyMongo

Write Data to MongoDB

On this page

  • Overview
  • Sample Application
  • Insert One
  • Insert Multiple
  • Update One
  • Update Multiple
  • Replace One
  • Delete One
  • Delete Multiple
  • Bulk Write

On this page, you can see copyable code examples that show common methods you can use to write data to MongoDB with PyMongo.

Tip

To learn more about any of the methods shown on this page, see the link provided in each section.

To use an example from this page, copy the code example into the sample application or your own application. Be sure to replace all placeholders in the code examples, such as <connection string URI>, with the relevant values for your MongoDB deployment.

You can use the following sample application to test the code examples on this page. To use the sample application, perform the following steps:

  1. Ensure you have PyMongo installed.

  2. Copy the following code and paste it into a new .py file.

  3. Copy a code example from this page and paste it on the specified lines in the file.

1import pymongo
2from pymongo import MongoClient
3
4try:
5 uri = "<connection string URI>"
6 client = MongoClient(uri)
7
8 database = client["<database name>"]
9 collection = database["<collection name>"]
10
11 # start example code here
12
13 # end example code here
14
15 client.close()
16
17except Exception as e:
18 raise Exception(
19 "The following error occurred: ", e)
result = collection.insert_one({ "<field name>" : "<value>" })
print(result.acknowledged)

To learn more about the insert_one() method, see the Insert Documents guide.

document_list = [
{ "<field name>" : "<value>" },
{ "<field name>" : "<value>" }
]
result = collection.insert_many(document_list)
print(result.acknowledged)

To learn more about the insert_many() method, see the Insert Documents guide.

query_filter = { "<field to match>" : "<value to match>" }
update_operation = { "$set" :
{ "<field name>" : "<value>" }
}
result = collection.update_one(query_filter, update_operation)
print(result.modified_count)

To learn more about the update_one() method, see the Update Documents guide.

query_filter = { "<field to match>" : "<value to match>" }
update_operation = { "$set" :
{ "<field name>" : "<value>" }
}
result = collection.update_many(query_filter, update_operation)
print(result.modified_count)

To learn more about the update_many() method, see the Update Documents guide.

query_filter = { "<field to match>" : "<value to match>" }
replace_document = { "<new document field name>" : "<new document value>" }
result = collection.replace_one(query_filter, replace_document)
print(result.modified_count)

To learn more about the replace_one() method, see the Replace Documents guide.

query_filter = { "<field to match>" : "<value to match>" }
result = collection.delete_one(query_filter)
print(result.deleted_count)

To learn more about the delete_one() method, see the Delete Documents guide.

query_filter = { "<field to match>" : "<value to match>" }
result = collection.delete_many(query_filter)
print(result.deleted_count)

To learn more about the delete_many() method, see the Delete Documents guide.

operations = [
pymongo.InsertOne(
{
"<field name>" : "<value>"
}
),
pymongo.UpdateMany(
{ "<field to match>" : "<value to match>" },
{ "$set" : { "<field name>" : "<value>" }},
),
pymongo.DeleteOne(
{ "<field to match>" : "<value to match>" }
),
]
result = collection.bulk_write(operations)
print(result)

To learn more about the bulk_write() method, see the Bulk Write guide.

← Databases and Collections