M
ONGODB INESRT
Insert Into Collection
A document in MongoDB is the same as a record in SQL databases.
To insert a record, or document as it is called in MongoDB, into a collection, we use the insert_one() method.
The first parameter of the insert_one() method is a dictionary containing the name(s) and value(s) of each field in the document you want to insert.
Insert a record in the "customers" collection:
Insert Multiple Documents
To insert multiple documents into a collection in MongoDB, we use the insert_many() method.
The first parameter of the insert_many() method is a list containing dictionaries with the data you want to insert:
If you do not specify an _id field, then MongoDB will add one for you and assign a unique id for each document.
In the example above no _id field was specified, so MongoDB assigned a unique _id for the record (document).
ONGODB INESRT
Insert Into Collection
A document in MongoDB is the same as a record in SQL databases.
To insert a record, or document as it is called in MongoDB, into a collection, we use the insert_one() method.
The first parameter of the insert_one() method is a dictionary containing the name(s) and value(s) of each field in the document you want to insert.
Insert a record in the "customers" collection:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydict = { "name": "John", "address": "Highway 37" }
x = mycol.insert_one(mydict) #The insert_one() method returns a InsertOneResult object
print(x.inserted_id) #inserted_id, that holds the id of the inserted document.
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydict = { "name": "John", "address": "Highway 37" }
x = mycol.insert_one(mydict) #The insert_one() method returns a InsertOneResult object
print(x.inserted_id) #inserted_id, that holds the id of the inserted document.
Insert Multiple Documents
To insert multiple documents into a collection in MongoDB, we use the insert_many() method.
The first parameter of the insert_many() method is a list containing dictionaries with the data you want to insert:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydict = { "name": "Kartik", "address": "California" }
x = mycol.insert_one(mydict)
print(x.inserted_id)
=mylist = [
{"_id": 2, "name": "Adi", "address": "New York" },
{"_id": 3, "name": "Nagar", "address": "Las Vegas" }
]
y = mycol.insert_many(mylist) #The insert_many() method returns a InsertManyResult object
print(y.inserted_ids) #inserted_ids, that holds the ids of the inserted documents.
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydict = { "name": "Kartik", "address": "California" }
x = mycol.insert_one(mydict)
print(x.inserted_id)
=mylist = [
{"_id": 2, "name": "Adi", "address": "New York" },
{"_id": 3, "name": "Nagar", "address": "Las Vegas" }
]
y = mycol.insert_many(mylist) #The insert_many() method returns a InsertManyResult object
print(y.inserted_ids) #inserted_ids, that holds the ids of the inserted documents.
6325e26da4ef8259d33e1a33
[2, 3]
[2, 3]
If you do not specify an _id field, then MongoDB will add one for you and assign a unique id for each document.
In the example above no _id field was specified, so MongoDB assigned a unique _id for the record (document).
Comments
Post a Comment