在 mongodb 中使用 $and 和 $match

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20469712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:31:54  来源:igfitidea点击:

using $and with $match in mongodb

mongodbmatch

提问by Manus

I am trying to use the following query in MongoDB but it is not working.

我正在尝试在 MongoDB 中使用以下查询,但它不起作用。

db.test.aggregate(
$match: {$and: [type: {$in: ["TOYS"]}, type: {$nin: ["BARBIE"]}, time: 
{$lt:ISODate("2013-12-09T00:00:00Z")}]}})

It says invalid character ":".

它说无效字符“:”。

Is it possible to use $and with $match? I have seen an example on this forum of $or with $match so I presumed this is possible.

是否可以将 $and 与 $match 一起使用?我在这个论坛上看到了一个 $or 和 $match 的例子,所以我认为这是可能的。

Thank you in advance for your help and guidance.

预先感谢您的帮助和指导。

回答by 4J41

$and with $match works just fine.

$and 与 $match 工作得很好。

You have syntax errors in your query. Try this.

您的查询中有语法错误。尝试这个。

db.test.aggregate([
                   { 
                     $match: {
                          $and: [ 
                              {type: {$in: ["TOYS"]}}, 
                              {type: {$nin: ["BARBIE"]}}, 
                              {time: {$lt:ISODate("2013-12-09T00:00:00Z")}}
                          ]
                     }
                   }
                  ])

And for what you are trying to do, you do not need an $and.

对于您要执行的操作,您不需要$and.

回答by Maximiliano Rios

If anybody wants to refuse my answer it's ok, this is a typical question with lack of research

如果有人想拒绝我的回答没关系,这是一个缺乏研究的典型问题

db.test.find( {$and: [ {"type": {$in: ["TOYS"]}}, 
                       {"type": {$nin: ["BARBIE"]}}, 
                       {"time": {$lt:ISODate("2013-12-09T00:00:00Z")}}
                     ]
})

AND works with FIND, receives an array of matches (but it's not a match instruction) Aggregation framework is for something completely different, it's like the word says, for aggregating (count, sum, avg, and so worth grouping or unwinding, etc)

AND 与 FIND 一起使用,接收一组匹配项(但它不是匹配指令)聚合框架用于完全不同的东西,就像这个词所说的那样,用于聚合(计数,总和,平均,因此值得分组或展开等)

回答by alfonsoolavarria

{
  $match: {
    $or:[
      {'sender':sender, 'recipient':recipient},
      {'recipient':sender,'sender':recipient}
    ]
  }
}

using $or

使用 $or