往 mongodb存儲(chǔ)的所有數(shù)據(jù),都是為了需要讀取的時(shí)候能夠取出。
但讀取除了按某一列比如分?jǐn)?shù): 排序 讀??;還會(huì)出現(xiàn)我只看某一段時(shí)間、某個(gè)班的條件篩選;還會(huì)出現(xiàn)我想看每個(gè)班平均分 聚合 求平均....等等多樣操作
這些操作都可以通過(guò) find_one()、find() 完成:
ret2find = collect.find_one() # {'_id': ObjectId('5ea780bf747e3e128470e485'), 'class_name': '高三(1)班', 'student_name': '張三', 'subject': '英語(yǔ)', 'score': 100, 'date': '20200301'} ret2find = collect.find() # pymongo.cursor.Cursor object at 0x0000024BBEBE15C8>
從上面的結(jié)果可以看出,find_one() 查詢得出單一字典;find()則是一個(gè)生成器對(duì)象能夠通過(guò) for val in ret2find: 遍歷取出
但能取出全部數(shù)據(jù)還不夠,查詢一般是會(huì)帶條件、甚至復(fù)雜的條件 —— 比如:查詢出 高三(1)班,張三 或 李四,成績(jī)大于90 的科目,該怎么做呢?
ret2find = collect.find({"class_name":"高三(1)班","score":{"$gt":90},"$or":[{"student_name":"張三"},{"student_name":"李四"}]}) for val in ret2find: print(val)
上面有兩個(gè)要點(diǎn):
{"class_name":"高三(1)班","score":{"$gt":90}}
這一段 寫法 表示 “高三(1)班 且 分?jǐn)?shù) > 90”;
而 $gt 比較操作符,表 大于意思,除 $gt 操作符以外還有:
符號(hào) | 含義 |
---|---|
$lt | 小于 |
$lte | 小于等于 |
$gt | 大于 |
$gte | 大于等于 |
$ne | 不等于 |
$in | 在范圍內(nèi) |
$nin | 不在范圍內(nèi) |
{"$or":[{"student_name":"張三"},{"student_name":"李四"}]}
這一段 寫法 表示 “學(xué)生名稱為 張三 或 李四”
而其中的 $or 邏輯操作符,用它來(lái)表示條件之間的關(guān)系。除了 $or 以外的邏輯操作符還有:
符號(hào) | 含義 |
---|---|
$and | 按條件取 交集 |
$not | 單個(gè)條件的 相反集合 |
$nor | 多個(gè)條件的 相反集合 |
$or | 多個(gè)條件的 并集 |
除了上述常規(guī)操作外,具體使用場(chǎng)景中我們還會(huì)用到:
符號(hào) | 含義 | 示例 | 示例含義 |
---|---|---|---|
$regex | 正則匹配 | {"student_name":{"regex":".∗三"}} | 學(xué)生名以 “三” 結(jié)尾 |
$expr | 允許查詢中使用 聚合表達(dá)式 | {"expr":{"gt":["spent","budget"]}} | 查詢 花費(fèi) 大于 預(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的模 |
更多 查詢操作符 可以點(diǎn)擊 查看官方文檔
在用pyhton遍歷mongo數(shù)據(jù)中時(shí)候,發(fā)限查詢到101行就會(huì)阻塞,如下
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,可能有一個(gè)限制閾值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ù)量不會(huì)超過(guò)最大BSON文檔大小。要覆蓋批處理的默認(rèn)大小,請(qǐng)參見batchSize()和limit()。
新版本3.4:類型為find()、aggregate()、listIndexes和listCollections的操作每批最多返回16兆字節(jié)。batchSize()可以執(zhí)行較小的限制,但不能執(zhí)行較大的限制。
find()和aggregate()操作的初始批處理大小默認(rèn)為101個(gè)文檔。針對(duì)生成的游標(biāo)發(fā)出的后續(xù)getMore操作沒(méi)有默認(rèn)的批處理大小,因此它們僅受16mb消息大小的限制。 對(duì)于包含沒(méi)有索引的排序操作的查詢,服務(wù)器必須在返回任何結(jié)果之前加載內(nèi)存中的所有文檔來(lá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ù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)PyMongo 查詢數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:西寧 湖北 潮州 盤錦 宜昌 佳木斯 上饒 珠海
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PyMongo 查詢數(shù)據(jù)的實(shí)現(xiàn)》,本文關(guān)鍵詞 PyMongo,查詢,數(shù)據(jù),的,實(shí)現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。