简体   繁体   English

$或mongodb 3.4中的合计

[英]$or aggregate in mongodb 3.4

I am trying hard to familiarize my self with pymongo and mongodb This query is very basic and very easy, but I am struggling to make it works 我正在努力使自己熟悉pymongo和mongodb。此查询非常基本且非常简单,但是我正在努力使其正常运行

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

I am using mongodb 3.4 which provides all of that pipeline stages. 我正在使用提供所有这些管道阶段的mongodb 3.4。 I don't know why it return null while the data is exists. 我不知道为什么数据存在​​时它返回null。

Your regular expression syntax is wrong. 您的正则表达式语法错误。 If you want to do a wildcard search you will need to use a regular expression operator $regex like so (the $options: 'i' part is to make the search case-insensitive): 如果要进行通配符搜索,则需要像这样使用正则表达式运算符$ 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