簡體   English   中英

$或mongodb 3.4中的合計

[英]$or aggregate in mongodb 3.4

我正在努力使自己熟悉pymongo和mongodb。此查詢非常基本且非常簡單,但是我正在努力使其正常運行

match = { "$match": {"$or" : [{"book.author" : ".*"+name+".*"},
                      {"book.editor" : ".*"+name+".*"},
                     {"book.illustrator" : ".*"+name+".*"}]}}
count = {"$count": "tot"}
cur = db.collection.aggregate([match, count])

我正在使用提供所有這些管道階段的mongodb 3.4。 我不知道為什么數據存在​​時它返回null。

您的正則表達式語法錯誤。 如果要進行通配符搜索,則需要像這樣使用正則表達式運算符$ regex$options: 'i'部分是使搜索不區分大小寫):

match = { "$match": {"$or" : [{"book.author" : {"$regex": name, "$options": "i"}},
                      {"book.editor" : {"$regex": name, "$options": "i"}},
                     {"book.illustrator" : {"$regex": name, "$options": "i"}}]}}
count = {"$count": "tot"}
cur = db.collection.aggregate([match, count])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM