Python MongoDB Insert Document
A document in MongoDB is similar to a record in an SQL table.
Insert into Collection
To insert a document into a collection, use the insert_one()
method. The first parameter of this method is a dictionary of name => value
pairs.
The following example inserts a document into the sites collection:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
mycol = mydb["sites"]
mydict = { "name": "tutorialpro", "alexa": "10000", "url": "https://www.tutorialpro.org" }
x = mycol.insert_one(mydict)
print(x)
print(x)
The output of the execution is:
<pymongo.results.InsertOneResult object at 0x10a34b288>
Return _id Field
The insert_one()
method returns an InsertOneResult object, which includes the inserted_id attribute, which is the id value of the inserted document.
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['tutorialprodb']
mycol = mydb["sites"]
mydict = { "name": "Google", "alexa": "1", "url": "https://www.google.com" }
x = mycol.insert_one(mydict)
print(x.inserted_id)
The output of the execution is:
5b2369cac315325f3698a1cf
If we do not specify an _id when inserting a document, MongoDB will add a unique id for each document.
Insert Multiple Documents
To insert multiple documents into a collection, use the insert_many()
method. The first parameter of this method is a list of dictionaries.
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
mycol = mydb["sites"]
mylist = [
{ "name": "Taobao", "alexa": "100", "url": "https://www.taobao.com" },
{ "name": "QQ", "alexa": "101", "url": "https://www.qq.com" },
{ "name": "Facebook", "alexa": "10", "url": "https://www.facebook.com" },
{ "name": "Zhihu", "alexa": "103", "url": "https://www.zhihu.com" },
{ "name": "Github", "alexa": "109", "url": "https://www.github.com" }
]
x = mycol.insert_many(mylist)
# Print the _id values of all inserted documents
print(x.inserted_ids)
The output is similar to the following:
[ObjectId('5b236aa9c315325f5236bbb6'), ObjectId('5b236aa9c315325f5236bbb7'), ObjectId('5b236aa9c315325f5236bbb8'), ObjectId('5b236aa9c315325f5236bbb9'), ObjectId('5b236aa9c315325f5236bbba')]
The insert_many()
method returns an InsertManyResult object, which includes the inserted_ids attribute, which holds the id values of all inserted documents.
After executing the above query, you can check if the data has been inserted in the command terminal.
Insert Multiple Documents with Specified _id
To insert multiple documents with specified id values, you can follow a similar approach as shown in the examples above, ensuring each document in the list has a specified id key. We can also specify our own id, insert, the following example inserts data into the site2 collection with _id specified by us:
Example
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["tutorialprodb"]
mycol = mydb["site2"]
mylist = [
{ "_id": 1, "name": "tutorialpro", "cn_name": "tutorialpro.org"},
{ "_id": 2, "name": "Google", "address": "Google Search"},
{ "_id": 3, "name": "Facebook", "address": "Facebook"},
{ "_id": 4, "name": "Taobao", "address": "Taobao"},
{ "_id": 5, "name": "Zhihu", "address": "Zhihu"}
]
x = mycol.insert_many(mylist)
# Print the _id values of all inserted documents
print(x.inserted_ids)
The output is:
[1, 2, 3, 4, 5]
After executing the above search, we can check in the command terminal whether the data has been inserted: