Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python MongoDB Insert Documents

Python MongoDB Insert Documents

Python Mongodb Python Mongodb

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

Insert into a collection

To insert a document into a collection, use the insert_one() method; its first parameter is a dictionary of name => value pairs.

The following example inserts into sites collection:

Example

#!/usr/bin/python3

import

pymongo

myclient

=

pymongo

.

MongoClient

(

"

mongodb://localhost:27017/

"

)

mydb

=

myclient

[

"

runoobdb

"

]

mycol

=

mydb

[

"

sites

"

]

mydict

= {

"

name

"

:

"

RUNOOB

"

,

"

alexa

"

:

"

10000

"

,

"

url

"

:

"

https://www.runoob.com

"

}

x

=

mycol

.

insert_one

(

mydict

)

print

(

x

)

print

(

x

)

The execution output is:


Return the _id field

The insert_one() method returns InsertOneResult object, which contains the inserted_id attribute, which is the id values.

Example

#!/usr/bin/python3

import

pymongo

myclient

=

pymongo

.

MongoClient

(

'

mongodb://localhost:27017/

'

)

mydb

=

myclient

[

'

runoobdb

'

]

mycol

=

mydb

[

"

sites

"

]

mydict

= {

"

name

"

:

"

Google

"

,

"

alexa

"

:

"

1

"

,

"

url

"

:

"

https://www.google.com

"

}

x

=

mycol

.

insert_one

(

mydict

)

print

(

x

.

inserted_id

)

The execution output is:

5b2369cac315325f3698a1cf

If we do not specify the _idwhen inserting a document, MongoDB adds a unique id。

Insert multiple documents

To insert multiple documents into a collection, use the insert_many() method; its first parameter is a list of dictionaries.

Example

#!/usr/bin/python3

import

pymongo

myclient

=

pymongo

.

MongoClient

(

"

mongodb://localhost:27017/

"

)

mydb

=

myclient

[

"

runoobdb

"

]

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 of all inserted documents

print

(

x

.

inserted_ids

)

The output is similar to the following:

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

The insert_many() method returns InsertManyResult object, which contains the inserted_ids attribute, which holds the id values.

After executing the above, 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 when inserting. In the following example, we insert into site2 collection,_id which we specify:

Example

#!/usr/bin/python3

import

pymongo

myclient

=

pymongo

.

MongoClient

(

"

mongodb://localhost:27017/

"

)

mydb

=

myclient

[

"

runoobdb

"

]

mycol

=

mydb

[

"

site2

"

]

mylist

=

[

{

"

_id

"

:

1

,

"

name

"

:

"

RUNOOB

"

,

"

cn_name

"

:

"

Runoob 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 of all inserted documents

print

(

x

.

inserted_ids

)

The output is:

[1, 2, 3, 4, 5]

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

Python Mongodb Python Mongodb