要在 MongoDB 中建立數據庫,首先要建立 MongoClient 對象,而後使用正確的 IP 地址和要建立的數據庫的名稱指定鏈接 URL。
若是數據庫不存在,MongoDB 將建立數據庫並創建鏈接。mongodb
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"]
要在 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 字段的值:ide
mydict = { "name": "Peter", "address": "Lowstreet 27" } x = mycol.insert_one(mydict) print(x.inserted_id)
要將多個文檔插入 MongoDB 中的集合,咱們使用 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) # 打印被插入文檔的 _id 值列表: print(x.inserted_ids)
若是您不但願 MongoDB 爲您的文檔分配惟一 id,則能夠在插入文檔時指定 _id 字段。
請記住,值必須是惟一的。兩個文件不能有相同的 _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 中的集合中選取數據,咱們能夠使用 find_one() 方法。文檔
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] x = mycol.find_one() print(x)
如需從 MongoDB 中的表中選取數據,咱們還能夠使用 find() 方法。io
實例
只返回姓名和地址,而不是 _ids:import
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] for x in mycol.find({},{ "_id": 0, "name": 1, "address": 1 }): print(x)
在集合中查找文檔時,您可以使用 query 對象過濾結果。
find() 方法的第一個參數是 query 對象,用於限定搜索。
實例
查找地址爲 "Park Lane 38" 的文檔:cli
import pymongo myclient = pymongo.MongoClient("mongodb://localhost:27017/") mydb = myclient["mydatabase"] mycol = mydb["customers"] myquery = { "address": "Park Lane 38" } mydoc = mycol.find(myquery) for x in mydoc: print(x)