Python MongoDB ドキュメント挿入
- 前のページ MongoDB 集合を作成
- 次のページ MongoDB ファインド
MongoDBのドキュメントはSQLデータベースのレコードと同じです。
コレクションに挿入
MongoDBにレコードまたは私たちが言うドキュメントを挿入するには、 insert_one()
メソッド。
insert_one()
メソッドの最初の引数は、ドキュメントに挿入したい各フィールド名と値を含む辞書です。
インスタンス
「customers」コレクションにレコードを挿入します:
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] mydict = { "name": "Bill", "address": "Highway 37" } x = mycol.insert_one(mydict)
が返されます。
insert_one()
メソッドはInsertOneResultオブジェクトを返し、そのオブジェクトには属性 inserted_id
、挿入されたドキュメントのidを保存するために使用されます。
インスタンス
「customers」コレクションに別のレコードを挿入し、_idフィールドの値を返します:
mydict = { "name": "Peter", "address": "Lowstreet 27" } x = mycol.insert_one(mydict) print(x.inserted_id)
指定していない場合、 _id
フィールドを指定していない場合、MongoDBは自動的に追加し、各ドキュメントにユニークなIDを割り当てます。
上の例では、 _id
フィールドがあり、MongoDBはレコード(ドキュメント)にユニークな_idを割り当てます。
複数のドキュメントを挿入
MongoDBのコレクションに複数のドキュメントを挿入するには、 insert_many()
メソッド。
insert_many()
メソッドの最初の引数は、挿入するデータを含む辞書のリストです:
インスタンス
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()
メソッドはInsertManyResultオブジェクトを返し、そのオブジェクトには属性があります inserted_ids
、インサートされたドキュメントのidを保存するために使用されます。
指定されたIDを持つ複数のドキュメントをインサートする
MongoDBがドキュメントにユニークな_idを割り当てることを望まない場合は、ドキュメントをインサートする際に_idフィールドを指定できます。
値はユニークでなければなりません。2つのファイルには同じ_idを持つものはありません。
インスタンス
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)
- 前のページ MongoDB 集合を作成
- 次のページ MongoDB ファインド