chapter
MongoDB similar records and documents in the SQL database.
Insert a document to a collection
To record (or MongoDB referred to in the document) is inserted into the collection, use the insert_one()
method.
insert_one()
The first argument is a dictionary containing the document you want to insert the name and value of each field.
Examples
Inserting records in the "customers" set:
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydict = { "name": "John", "address": "Highway 37" }
x = mycol.insert_one(mydict)
Returns the _id field
insert_one()
Method returns the InsertOneResult
object that has a inserted_id
property, saved id insert the document.
Examples
Insert a record in the "customers" set, and returns the _id
value of the field:
mydict = { "name": "Peter", "address": "Lowstreet 27" }
x = mycol.insert_one(mydict)
print(x.inserted_id)
When inserted into the document, if not specified _id
, it will automatically assign a unique id.
In the above example, _id field is not specified, MongoDB is recorded (document) is assigned a unique _id.
Insert multiple documents
To insert a set of multiple documents, you can use the insert_many()
method.
insert_many()
The first parameter is a dictionary method list containing data to be inserted:
Examples
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)
# 打印文档_id值列表:
print(x.inserted_ids)
insert_many()
Method returns the InsertManyResult
object that has a inserted_ids
property, save all inserted into the document's id
Insert multiple documents with the specified id
When inserted into the document, if you do not want to assign MongoDB is a document id, you can specify _id
fields.
Note, _id value must be unique, any two documents can have the same _id.
Examples
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)
# 打印文档_id值列表:
print(x.inserted_ids)