Python MongoDB Insert Document
- Previous Page MongoDB Create Collection
- Next Page MongoDB Find
Documents in MongoDB are the same as records in SQL databases.
Insert into collection
To insert a record or what we call a document into a collection in MongoDB, we use insert_one()
Method.
insert_one()
The first parameter of the method is a dictionary containing each field name and value that you want to insert into the document.
instance
Insert a record into the "customers" collection:
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] mydict = { "name": "Bill", "address": "Highway 37" } x = mycol.insert_one(mydict)
returns the _id field
insert_one()
The method returns an InsertOneResult object, which has properties inserted_id
, used to save the id of the inserted document.
instance
Insert another record into the "customers" collection and return the value of the _id field:
mydict = { "name": "Peter", "address": "Lowstreet 27" } x = mycol.insert_one(mydict) print(x.inserted_id)
If you do not specify _id
field was not specified, so MongoDB will add one for you and assign a unique ID to each document.
In the example above, the _id
field, so MongoDB assigns a unique _id to each record (document).
Insert multiple documents
To insert multiple documents into a collection in MongoDB, we use insert_many()
Method.
insert_many()
The first parameter of the method is a list containing dictionaries with the data to be inserted:
instance
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] mylist = [ {"name": "Amy", "address": "Apple st 652"}, {"name": "Hannah", "address": "Mountain 21"}, {"name": "Michael", "address": "Valley 345"}, {"name": "Sandy", "address": "Ocean blvd 2"}, {"name": "Betty", "address": "Green Grass 1"}, {"name": "Richard", "address": "Sky st 331"}, {"name": "Susan", "address": "One way 98"}, { "name": "Vicky", "address": "Yellow Garden 2"}, { "name": "Ben", "address": "Park Lane 38"}, { "name": "William", "address": "Central st 954"}, { "name": "Chuck", "address": "Main Road 989"}, { "name": "Viola", "address": "Sideway 1633"} ] x = mycol.insert_many(mylist) # Print the list of _id values of the inserted documents: print(x.inserted_ids)
insert_many()
The method returns an InsertManyResult object, which has properties inserted_ids
used to save the id of the inserted document.
Insert multiple documents with specified ID
If you do not want MongoDB to assign a unique id to your document, you can specify the _id field when inserting the document.
Remember that the value must be unique. Two files cannot have the same _id.
instance
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] mylist = [ { "_id": 1, "name": "John", "address": "Highway 37"}, { "_id": 2, "name": "Peter", "address": "Lowstreet 27"}, { "_id": 3, "name": "Amy", "address": "Apple st 652"}, { "_id": 4, "name": "Hannah", "address": "Mountain 21"}, { "_id": 5, "name": "Michael", "address": "Valley 345"}, { "_id": 6, "name": "Sandy", "address": "Ocean blvd 2"}, { "_id": 7, "name": "Betty", "address": "Green Grass 1"}, {"_id": 8, "name": "Richard", "address": "Sky st 331"} {"_id": 9, "name": "Susan", "address": "One way 98"}, {"_id": 10, "name": "Vicky", "address": "Yellow Garden 2"}, {"_id": 11, "name": "Ben", "address": "Park Lane 38"}, {"_id": 12, "name": "William", "address": "Central st 954"}, {"_id": 13, "name": "Chuck", "address": "Main Road 989"}, {"_id": 14, "name": "Viola", "address": "Sideway 1633"} ] x = mycol.insert_many(mylist) # Print the list of _id values of the inserted documents: print(x.inserted_ids)
- Previous Page MongoDB Create Collection
- Next Page MongoDB Find