PyMongo 查詢數(shù)據(jù)的實現(xiàn)
查詢數(shù)據(jù)
往 mongodb存儲的所有數(shù)據(jù),都是為了需要讀取的時候能夠取出。
但讀取除了按某一列比如分?jǐn)?shù): 排序 讀?。贿€會出現(xiàn)我只看某一段時間、某個班的條件篩選;還會出現(xiàn)我想看每個班平均分 聚合 求平均....等等多樣操作
這些操作都可以通過 find_one()、find() 完成:
ret2find = collect.find_one() # {'_id': ObjectId('5ea780bf747e3e128470e485'), 'class_name': '高三(1)班', 'student_name': '張三', 'subject': '英語', 'score': 100, 'date': '20200301'} ret2find = collect.find() # <pymongo.cursor.Cursor object at 0x0000024BBEBE15C8>
從上面的結(jié)果可以看出,find_one() 查詢得出單一字典;find()則是一個生成器對象能夠通過 for val in ret2find: 遍歷取出
設(shè)置查詢條件
但能取出全部數(shù)據(jù)還不夠,查詢一般是會帶條件、甚至復(fù)雜的條件 —— 比如:查詢出 高三(1)班,張三 或 李四,成績大于90 的科目,該怎么做呢?
ret2find = collect.find({"class_name":"高三(1)班","score":{"$gt":90},"$or":[{"student_name":"張三"},{"student_name":"李四"}]}) for val in ret2find: print(val)
上面有兩個要點:
{"class_name":"高三(1)班","score":{"$gt":90}}
這一段 寫法 表示 “高三(1)班 且 分?jǐn)?shù) > 90”;
而 $gt 比較操作符,表 大于意思,除 $gt 操作符以外還有:
符號 | 含義 |
---|---|
$lt | 小于 |
$lte | 小于等于 |
$gt | 大于 |
$gte | 大于等于 |
$ne | 不等于 |
$in | 在范圍內(nèi) |
$nin | 不在范圍內(nèi) |
{"$or":[{"student_name":"張三"},{"student_name":"李四"}]}
這一段 寫法 表示 “學(xué)生名稱為 張三 或 李四”
而其中的 $or 邏輯操作符,用它來表示條件之間的關(guān)系。除了 $or 以外的邏輯操作符還有:
符號 | 含義 |
---|---|
$and | 按條件取 交集 |
$not | 單個條件的 相反集合 |
$nor | 多個條件的 相反集合 |
$or | 多個條件的 并集 |
更多查詢操作
除了上述常規(guī)操作外,具體使用場景中我們還會用到:
符號 | 含義 | 示例 | 示例含義 |
---|---|---|---|
$regex | 正則匹配 | {"student_name":{"regex":".∗三"}} | 學(xué)生名以 “三” 結(jié)尾 |
$expr | 允許查詢中使用 聚合表達式 | {"expr":{"gt":["spent","budget"]}} | 查詢 花費 大于 預(yù)算 的超支記錄 |
$exists | 屬性是否存在 | {"date":{"$exists": True}} | date屬性存在 |
$exists | 屬性是否存在 | {"date":{"$exists": True}} | date屬性存在 |
$type | 類型判斷 | {"score":{"$type":"int"}} | score的類型為int |
$mod | 取模操作 | {'score': {'$mod': [5, 0]}} | 分?jǐn)?shù)取5、0的模 |
更多 查詢操作符 可以點擊 查看官方文檔
PS:pymongo最大查詢限制
在用pyhton遍歷mongo數(shù)據(jù)中時候,發(fā)限查詢到101行就會阻塞,如下
lista_a = [] for info in db.get_collection("dbs").find(): lista_a.append(info) print("info nums=",len(info)) '''結(jié)果顯示''' '''info nums=101'''
分析原因:mongodb的find()方法返回游標(biāo)cursor,可能有一個限制閾值101,參考文檔,如下
原文:
The MongoDB server returns the query results in batches. The amount of data in the batch will not exceed the maximum BSON document size. To override the default size of the batch, see batchSize() and limit().
New in version 3.4: Operations of type find(), aggregate(), listIndexes, and listCollections return a maximum of 16 megabytes per batch. batchSize() can enforce a smaller limit, but not a larger one.
find() and aggregate() operations have an initial batch size of 101 documents by default. Subsequent getMore operations issued against the resulting cursor have no default batch size, so they are limited only by the 16 megabyte message size.
For queries that include a sort operation without an index, the server must load all the documents in memory to perform the sort before returning any results.
翻譯:
MongoDB服務(wù)器批量返回查詢結(jié)果。批處理中的數(shù)據(jù)量不會超過最大BSON文檔大小。要覆蓋批處理的默認(rèn)大小,請參見batchSize()和limit()。
新版本3.4:類型為find()、aggregate()、listIndexes和listCollections的操作每批最多返回16兆字節(jié)。batchSize()可以執(zhí)行較小的限制,但不能執(zhí)行較大的限制。
find()和aggregate()操作的初始批處理大小默認(rèn)為101個文檔。針對生成的游標(biāo)發(fā)出的后續(xù)getMore操作沒有默認(rèn)的批處理大小,因此它們僅受16mb消息大小的限制。 對于包含沒有索引的排序操作的查詢,服務(wù)器必須在返回任何結(jié)果之前加載內(nèi)存中的所有文檔來執(zhí)行排序。
解決方案
lista_a = [] for info in db.get_collection("dbs").find().batch_size1(5000): #修改最大限制閾 lista_a.append(info) print("info nums=",len(info))
但是這種方法是每次游標(biāo)返回5000條數(shù)據(jù),循環(huán)遍歷,如果單詞查找50000次應(yīng)該怎么寫呢?如下
lista_a = [] cousor=db.get_collection("dbs").find().batch_size1(5000) for i in range(50000): #修改最大限制閾 lista_a.append(next(cousor))
到此這篇關(guān)于PyMongo 查詢數(shù)據(jù)的實現(xiàn)的文章就介紹到這了,更多相關(guān)PyMongo 查詢數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
centos6.5安裝python3.7.1之后無法使用pip的解決方案
今天小編就為大家分享一篇關(guān)于centos6.5安裝python3.7.1之后無法使用pip的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02python lambda函數(shù)及三個常用的高階函數(shù)
這篇文章主要介紹了python lambda函數(shù)及三個常用的高階函數(shù),本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02python 中的列表生成式、生成器表達式、模塊導(dǎo)入
這篇文章主要介紹了python中的列表生成式、生成器表達式、模塊導(dǎo)入 ,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06