pymongo查詢技巧




from pymongo import MongoClient
mdb = MongoClient('120.xxx.xxx.xxx:20002', username='xxx', password='xxx')

# 數據240萬
# no_cursor_timeout=True代表連接不中斷,連續取
# batch_size = 2000代表每批次取2000條
# limit = 100限制100條
# skip代表跳過多少
# 比如在三台機器執行任務,一台直接取100萬,第二台跳過100萬限制取100萬,第三台跳過200萬
# find() 里面第一個花括號代表查詢條件,第二個代表返回結果的字段(0不返回,1返回),在大量數據操作的時候很明顯可以提升性能
images = mdb['testdb']['image'].find({"image_size.height": {"$exists": True}}, {"url": 1, "other": 0}, no_cursor_timeout=True).batch_size(2000).limit(100)
images = mdb['testdb']['image'].find({"image_size.height": {"$exists": True}}, {"url": 1, "other": 0}, no_cursor_timeout=True).batch_size(2000).skip(100)

 

根據圖像ID批量返回數據:

image_ids = ["xxx", "yyy", "zzz"]

image_infos = mdb['testdb']['image_info'].find({"image_id": {"$in": image_ids}})

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM