Python MongoDB Insert Document
A actual database and collection will be created only after adding a document into it.
A MongoDB document is equal to a SQL database record.
Insert Document Into Collections
The insert_one()
function is used to insert a record or document as it is called in mongoDB in a collection.
A document is a dictionary with the name(s) and values of each field.
Example :- Insert a record in the "emp" collection :
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['mydatabase']
mycol = mydb["emp"]
mydict = { "name": "aa", "city": "paris" }
x = mycol.insert_one(mydict)
print(x)
Output :-
Return the _id Field
The insert_one()
method returns an object that has inserted_id
, that carries the identification of the document being inserted.
Example :- Insert another record in the collection "emp" and return fields in _id
values :
import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017/')
mydb = myclient['mydatabase']
mycol = mydb["emp"]
mydict = { "name": "bb", "city": "London" }
x = mycol.insert_one(mydict)
print(x.inserted_id)
Output :-
If no _id
field is specified, then MongoDB adds one to you for every document and sets a unique id for it.
After executing above program, the "emp" collection will look like :
{'_id': ObjectId('612b982a2040b3dc1d31f6b1'), 'name': 'bb', 'city': 'London'}
No _id
field was set in the sample above, so that MongoDB assigned a unique _id
to the document.
Related Links
Insert Multiple Documents
In MongoDB, we utilise the insert_many()
method to insert several documents at once into a collection.
The first parameter of the insert_many()
method is a dictionary which contains
list holding the data to be inserted.
The inserted_ids()
method returns all ids of inserted document by using insert_many()
method.
Example :- Inserting three documents at once :
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["emp"]
mylist = [
{ "name": "cc", "city": "paris"},
{ "name": "dd", "city": "New york"},
{ "name": "ee", "city": "tokyo"}
]
x = mycol.insert_many(mylist)
# print list of the _id values of the inserted documents:
print(x.inserted_ids)
Output :-
The insert_many()
method produces an object InsertManyResult with a property, inserted_ids
containing the ids of the documents inserted.
Insert Multiple Documents, with Specified ID's
You can set a _id
field when inserting the document if you do not like MongoDB to assign unique id to your document(s).
Remember:
- Each
_id
value must be unique - Two documents cannot have same
_id
value
Example : Inserting multiple documents with custom _id
value :
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["emp"]
mylist = [
{ "_id": 1, "name": "ff", "city": "paris"},
{ "_id": 2, "name": "gg", "city": "tokyo"},
{ "_id": 3, "name": "hh", "city": "london"}
]
x = mycol.insert_many(mylist)
# print a list of the _id values of the inserted documents:
print(x.inserted_ids)
Output :-
After executing the above program, the "emp" collection will look like :
{'_id': ObjectId('612b982a2040b3dc1d31f6b1'), 'name': 'bb', 'city': 'London'}
{'_id': ObjectId('612b9d1fca9e99c8fc711749'), 'name': 'cc', 'city': 'paris'}
{'_id': ObjectId('612b9d1fca9e99c8fc71174a'), 'name': 'dd', 'city': 'New york'}
{'_id': ObjectId('612b9d1fca9e99c8fc71174b'), 'name': 'ee', 'city': 'tokyo'}
{'_id': 1, 'name': 'ff', 'city': 'paris'}
{'_id': 2, 'name': 'gg', 'city': 'tokyo'}
{'_id': 3, 'name': 'hh', 'city': 'london'}
Related Links