符號(hào) | 含義 |
---|---|
$lt | 小于 |
$lte | 小于等于 |
$gt | 大于 |
$gte | 大于等于 |
$ne | 不等于 |
$in | 在范圍內(nèi) |
$nin | 不在范圍內(nèi) |
{"$or":[{"student_name":"張三"},{"student_name":"李四"}]}
這一段 寫(xiě)法 表示 “學(xué)生名稱(chēng)為 張三 或 李四”
而其中的 $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 | 允許查詢(xún)中使用 聚合表達(dá)式 | {"expr":{"gt":["spent","budget"]}} | 查詢(xún) 花費(fèi) 大于 預(yù)算 的超支記錄 |
$exists | 屬性是否存在 | {"date":{"$exists": True}} | date屬性存在 |
$exists | 屬性是否存在 | {"date":{"$exists": True}} | date屬性存在 |
$type | 類(lèi)型判斷 | {"score":{"$type":"int"}} | score的類(lèi)型為int |
$mod | 取模操作 | {'score': {'$mod': [5, 0]}} | 分?jǐn)?shù)取5、0的模 |
更多 查詢(xún)操作符 可以點(diǎn)擊 查看官方文檔
在用pyhton遍歷mongo數(shù)據(jù)中時(shí)候,發(fā)限查詢(xún)到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ù)器批量返回查詢(xún)結(jié)果。批處理中的數(shù)據(jù)量不會(huì)超過(guò)最大BSON文檔大小。要覆蓋批處理的默認(rèn)大小,請(qǐng)參見(jiàn)batchSize()和limit()。
新版本3.4:類(lèi)型為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)有索引的排序操作的查詢(xún),服務(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)該怎么寫(xiě)呢?如下
lista_a = [] cousor=db.get_collection("dbs").find().batch_size1(5000) for i in range(50000): #修改最大限制閾 lista_a.append(next(cousor))
到此這篇關(guān)于PyMongo 查詢(xún)數(shù)據(jù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)PyMongo 查詢(xún)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:西寧 湖北 潮州 盤(pán)錦 宜昌 佳木斯 上饒 珠海
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PyMongo 查詢(xún)數(shù)據(jù)的實(shí)現(xiàn)》,本文關(guān)鍵詞 PyMongo,查詢(xún),數(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)。