Python MongoDB 插入文檔

MongoDB中的文檔與SQL數據庫中的記錄類似。

插入文檔到集合

要將記錄(或MongoDB中所稱的文檔)插入到集合中,可使用insert_one()方法。

insert_one()方法的第一個參數是一個字典,包含了要插入的文檔中,每個字段的名稱和值。

示例

在“customers”集合內插入記錄:

<code>

myclient

= pymongo.MongoClient(

"mongodb://localhost:27017/"

)

mydb

= myclient[

"mydatabase"

]

mycol

= mydb[

"customers"

]

mydict

= {

"name"

:

"John"

,

"address"

:

"Highway 37"

}

x

= mycol.insert_one(mydict) /<code>

複製

返回_id字段

insert_one()方法返回InsertOneResult對象,該對象有一個inserted_id屬性,保存了插入文檔的id。

示例

在“customers”集合中插入一條記錄,並返回_id字段的值:

<code>mydict = { 

"name"

:

"Peter"

,

"address"

:

"Lowstreet 27"

} x = mycol.insert_one(mydict) print(x.inserted_id) /<code>

複製

插入文檔時,如果沒有指定_id,將自動分配一個唯一的id。

上面的例子中,沒有指定_id字段,MongoDB為記錄(文檔)分配了一個唯一_id。

插入多個文檔

要將多個文檔插入集合,可以使用insert_many()方法。

insert_many()方法的第一個參數是一個字典列表,包含了要插入的數據:

示例

<code>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(x.inserted_ids) /<code>

複製

insert_many()方法返回InsertManyResult對象,該對象有一個inserted_ids屬性,保存了所有插入文檔的id

插入多個具有指定id的文檔

插入文檔時,如果不希望MongoDB為文檔分配id,可以指定_id字段。

注意,_id值必須是惟一的,任意兩個文檔不能具有相同的_id。

示例

<code>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(x.inserted_ids)/<code>


Python MongoDB 插入文檔


分享到:


相關文章: