Python Mongodb Insert Document

Python Mongodb Python Mongodb

A document in MongoDB is similar to a record in an SQL table.

Insert Into Collection

To insert a document into a collection, use theinsert_one()method, whose first parameter is a dictionaryname => valuepair.

The following examplesitesinserts a document into the collection:

Example

#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["exampledb"] mycol = mydb["sites"] mydict = { "name": "EXAMPLE", "alexa": "10000", "url": "https://www.example.com" } x = mycol.insert_one(mydict) print(x) print(x)

The output after execution is:

<pymongo.results.InsertOneResult object at 0x10a34b288>

Return the _id Field

insert_one()The method returnsInsertOneResultan object, which containsinserted_idattribute, which is the inserted document'sidvalue.

Example

#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient('mongodb://localhost:27017/') mydb = myclient['exampledb'] mycol = mydb["sites"] mydict = { "name": "Google", "alexa": "1", "url": "https://www.google.com" } x = mycol.insert_one(mydict) print(x.inserted_id)

The output after execution is:

5b2369cac315325f3698a1cf

If we do not specify_id, MongoDB will add a uniqueid。

Insert Multiple Documents

To insert multiple documents into a collection, use theinsert_many()method, whose first parameter is a list of dictionaries.

Example

#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["exampledb"] mycol = mydb["sites"] mylist = [ { "name": "Taobao", "alexa": "100", "url": "https://www.taobao.com" }, { "name": "QQ", "alexa": "101", "url": "https://www.qq.com" }, { "name": "Facebook", "alexa": "10", "url": "https://www.facebook.com" }, { "name": "Zhihu", "alexa": "103", "url": "https://www.zhihu.com" }, { "name": "Github", "alexa": "109", "url": "https://www.github.com" } ] x = mycol.insert_many(mylist) # Output the _id values corresponding to all inserted documents print(x.inserted_ids)

The output result is similar to the following:

[ObjectId('5b236aa9c315325f5236bbb6'), ObjectId('5b236aa9c315325f5236bbb7'), ObjectId('5b236aa9c315325f5236bbb8'), ObjectId('5b236aa9c315325f5236bbb9'), ObjectId('5b236aa9c315325f5236bbba')]

insert_many()The method returnsInsertManyResultan object, which containsinserted_idsattribute, which stores all the inserted documents'idvalues.

After executing the above search, we can check in the command terminal whether the data has been inserted:

Insert Multiple Documents with Specified _id

We can also specify the id ourselves and insert. In the following example, wesite2insert data into the collection,_idas specified by us:

Example

#!/usr/bin/python3 import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["exampledb"] mycol = mydb["site2"] mylist = [ { "_id": 1, "name": "EXAMPLE", "cn_name": "Example Tutorial"}, { "_id": 2, "name": "Google", "address": "Google Search"}, { "_id": 3, "name": "Facebook", "address": "Facebook"}, { "_id": 4, "name": "Taobao", "address": "Taobao"}, { "_id": 5, "name": "Zhihu", "address": "Zhihu"} ] x = mycol.insert_many(mylist) # Output the _id values corresponding to all inserted documents print(x.inserted_ids)

The output result is:

[1, 2, 3, 4, 5]

After executing the above search, we can check in the command terminal whether the data has been inserted:

Python Mongodb Python Mongodb

Other Extensions