pymongo 常用操作函式
1. 連線資料庫
要使用pymongo最先應該做的事就是先連上執行中的 mongod 。
- 建立一個 .py 檔案,首先匯入 pymongo:
from pymongo import MongoClient
- 建立一個連線到 mongod 到客戶端:
client = MongoClient()
或者:
client = MongoClient("mongodb://mongodb0.example.net:27019")
- 連線資料庫:
# 假設要連線的資料庫名為 primer
db = client.primer
或者:
db = client['primer']
- 連線到對應的資料集:
coll = db.dataset coll = db['dataset']
至此,已經完整對連線了資料庫和資料集,完成了初識化的操作。
2. 插入資料
insert_one(document)
insert_many(documents, ordered=True)
insert_one(document)
在 pymongo 中的插入函式並不像 mongo shell 中完全一樣,所以需要注意一下:
from datetime import datetime result = db.restaurants.insert_one( { "address": { "street": "2 Avenue", "zipcode": "10075", "building": "1480", "coord": [-73.9557413, 40.7720266] }, "borough": "Manhattan", "cuisine": "Italian", "grades": [ { "date": datetime.strptime("2014-10-01", "%Y-%m-%d"), "grade": "A", "score": 11 }, { "date": datetime.strptime("2014-01-16", "%Y-%m-%d"), "grade": "B", "score": 17 } ], "name": "Vella", "restaurant_id": "41704620" } )
其中返回的結果:result 中是一個:InsertOneResult 類:class pymongo.results.InsertOneResult(inserted_id, acknowledged)
其中 inserted_id
是插入的元素多 _id
值。
insert_many(documents, ordered=True)
result = db.test.insert_many([{'x': i} for i in range(2)])
查詢資料
find(filter=None, projection=None, skip=0, limit=0,
no_cursor_timeout=False, cursor_type=CursorType.NON_TAILABLE,
sort=None, allow_partial_results=False, oplog_replay=False,
modifiers=None, manipulate=True)
find_one(filter_or_id=None, *args, **kwargs)
find
find 查詢出來的是一個列表集合。
cursor = db.restaurants.find()
for document in cursor:
print(document)
# 查詢欄位是最上層的
cursor = db.restaurants.find({"borough": "Manhattan"})
# 查詢欄位在內層巢狀中
cursor = db.restaurants.find({"address.zipcode": "10075"})
- 操作符查詢
cursor = db.restaurants.find({"grades.score": {"$gt": 30}})
cursor = db.restaurants.find({"grades.score": {"$lt": 10}})
# AND
cursor = db.restaurants.find({"cuisine": "Italian", "address.zipcode": "10075"})
cursor = db.restaurants.find(
{"$or": [{"cuisine": "Italian"}, {"address.zipcode": "10075"}]})
-
find_one
返回的是一個JSON式文件,所以可以直接使用! sort
排序時要特別注意,使用的並不是和mongo shell的一樣,而是使用了列表,
當排序的標準只有一個,且是遞增時,可以直接寫在函式引數中:
pymongo.ASCENDING = 1
pymongo.DESCENDING = -1
cursor = db.restaurants.find().sort("borough")
cursor = db.restaurants.find().sort([
("borough", pymongo.ASCENDING),
("address.zipcode", pymongo.DESCENDING)
])
更新文件
更新文件的函式有三個(不能更新 _id
欄位)
update_one(filter, update, upsert=False)
update_many(filter, update, upsert=False)
replace_one(filter, replacement, upsert=False)
find_one_and_update(filter, update, projection=None, sort=None, return_document=ReturnDocument.BEFORE, **kwargs)
update_one
返回結果是一個:UpdateResult
,如果查詢到多個匹配,則只更新
第一個!
result = db.restaurants.update_one(
{"name": "Juni"},
{
"$set": {
"cuisine": "American (New)"
},
"$currentDate": {"lastModified": True}
}
)
result.matched_count
10
result.modified_count
1
update_many
查詢到多少匹配,就更新多少。
result = db.restaurants.update_many(
{"address.zipcode": "10016", "cuisine": "Other"},
{
"$set": {"cuisine": "Category To Be Determined"},
"$currentDate": {"lastModified": True}
}
)
result.matched_count
20
result.modified_count
20
replace_one
result = db.restaurants.replace_one(
{"restaurant_id": "41704620"},
{
"name": "Vella 2",
"address": {
"coord": [-73.9557413, 40.7720266],
"building": "1480",
"street": "2 Avenue",
"zipcode": "10075"
}
}
)
result.matched_count
1
result.modified_count
1
find_one_and_update
返回更新前的文件
db.test.find_one_and_update(
{'_id': 665}, {'$inc': {'count': 1}, '$set': {'done': True}})
{u'_id': 665, u'done': False, u'count': 25}}
刪除文件
刪除時主要有兩個:
delete_one(filter)
delete_many(filter)
drop()
find_one_and_delete(filter, projection=None, sort=None, kwargs)
find_one_and_replace(filter, replacement, projection=None, sort=None, return_document=ReturnDocument.BEFORE, kwargs)
delete_one
result = db.test.delete_one({'x': 1})
result.deleted_count
1
delete_many
result = db.restaurants.delete_many({"borough": "Manhattan"})
result.deleted_count
10259
# 刪除全部
result = db.restaurants.delete_many({})
drop()
刪除整個集合,是drop_collection()
的別名
db.restaurants.drop()
find_one_and_delete
db.test.count({'x': 1})
2
db.test.find_one_and_delete({'x': 1})
{u'x': 1, u'_id': ObjectId('54f4e12bfba5220aa4d6dee8')}
db.test.count({'x': 1})
find_one_and_replace
>>> for doc in db.test.find({}):
... print(doc)
...
{u'x': 1, u'_id': 0}
{u'x': 1, u'_id': 1}
{u'x': 1, u'_id': 2}
>>> db.test.find_one_and_replace({'x': 1}, {'y': 1})
{u'x': 1, u'_id': 0}
>>> for doc in db.test.find({}):
... print(doc)
...
{u'y': 1, u'_id': 0}
{u'x': 1, u'_id': 1}
{u'x': 1, u'_id': 2}
索引操作
索引主要有建立索引和刪除索引:
create_index(keys, **kwargs)
create_indexes(indexes)
drop_index(index_or_name)
drop_indexes()
reindex()
list_indexes()
index_information()
create_index
my_collection.create_index("mike")
my_collection.create_index([("mike", pymongo.DESCENDING),
... ("eliot", pymongo.ASCENDING)])
my_collection.create_index([("mike", pymongo.DESCENDING)],
... background=True)
create_indexes
>>> from pymongo import IndexModel, ASCENDING, DESCENDING
>>> index1 = IndexModel([("hello", DESCENDING),
... ("world", ASCENDING)], name="hello_world")
>>> index2 = IndexModel([("goodbye", DESCENDING)])
>>> db.test.create_indexes([index1, index2])
["hello_world"]
drop_index
index_or_name
: 索引編號或者索引的name
my_collection.drop_index("mike")
-
drop_indexs
刪除所有索引 reindex
重構索引,儘量少用,如果集合比較大多話,會很耗時耗力.
for index in db.test.list_indexes():
... print(index)
...
SON([(u'v', 1), (u'key', SON([(u'_id', 1)])),
(u'name', u'_id_'), (u'ns', u'test.test')])