Python MongoDB Insert Document

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.

You can also search for these topics, python mongodb insert multiple document, insert the document in another collection using python, how to duplicate the inserted documets in python mangodb, example for python mangodb document insertion, empty the inserted python mangodb document, insert document in python mangodb the field, document key insertion into the python mangodb, define the limit and object of the insert document using python mangodb.

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 :-

<pymongo.results.InsertOneResult object at 0x00000209197B7D40>

You can also search for these topics, python insert into collection between element, python insert in collection group, key values and key update to insert into collection of python mangodb, collections not present in python collections insertions, collections not exists in python insert, python insert collections to objects and values, collections of python mangodb insert the text file.

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 :-

612b982a2040b3dc1d31f6b1

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('612b96ddadca9fc356df606f'), 'name': 'aa', 'city': 'paris'}
{'_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.



You can also search for these topics, python return the id field value, current user and process to retrun the id field using python, default value in python id field returned, python dynamically return id field, find the error of the python id field return, example for python return the id field, insert the id field using python should return.

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 :-

[ObjectId('612b9d1fca9e99c8fc711749'), ObjectId('612b9d1fca9e99c8fc71174a'), ObjectId('612b9d1fca9e99c8fc71174b')]

The insert_many() method produces an object InsertManyResult with a property, inserted_ids containing the ids of the documents inserted.

You can also search for these topics, mongodb insert multiple documents at once python, multiple documents exceptions in python insert, python items to set multiple insert documents, keys insert many documents using python, python multiple documents list insert, python request and update multiple documents to insert, example for python insert multiple documents.

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 :-

[1, 2, 3]

After executing the above program, the "emp" collection will look like :

{'_id': ObjectId('612b96ddadca9fc356df606f'), 'name': 'aa', 'city': 'paris'}
{'_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'}



You can also search for these topics, python insert multiple documents with specified ids at once, python insert multiple documents with specified ids contains, example for python Insert Multiple Documents, with Specified IDs, field of specified id of python insert many documents, list the mongodb multiple documents with specified id fields.