2016-03-15 193 views
0

我正在使用与$match,$project$geonear流水线运算符的汇总流水线。

在每个收集的我场为工作日,如:

  1. repeat_every_monday =true

  2. repeat_every_tuesday=true

所有工作日相同的序列。

现在,如果今天是星期二,然后我不得不把$or条件在我的管道运营商也获取所有的纪录,之前的数据,其中被标记为repeat_every_tuesday=true

我想表达的类似条件操作

if (name=="pankaj" and "test"=="false" || "test"==true) 

这是我迄今所做的:

db.test.aggregate([ 
    {'$match':{"name":"pankaj","test":false,}}, 
    {'$project':{_id:1‌​,name:1,test:1,sir:1}} 
]).pretty() 

在这里我必须把$or

I have test collection and data is as below: 
    db.test.find().pretty() 
{ 
    "_id" : ObjectId("56e79492f286d94702cf4e31"), 
    "name" : "pankaj", 
    "test" : true, 
    "sir" : "cheema" 
} 
{ 
    "_id" : ObjectId("56e7abb3f286d94702cf4e32"), 
    "name" : "pankaj", 
    "test" : false, 
    "sir" : "cheema" 
} 


I want a aggregate query with $match which should give me result where  name is =pankaj and test is either true or false . 
    For example in sql : 
    Select * from test where name=pankaj or test=false or test =true. 
+0

好了,究竟是你的问题? – Philipp

+0

我想用$或我的聚合。 –

+0

db.test.aggregate([{'$ match':{“name”:“pankaj”,“test”:false,}},{'$ project':{_ id:1,name:1,test:1 ,先生:1}}])。漂亮()我必须把$或 –

回答

0

使用这样

db.articles.aggregate([ 
    { $match: { $or: [ {"$and":["name":"pankaj","test":"false"]}, 
    {"test":"true"}]}}, 

]); 
+0

你的$和'是多余的。您只需传递一个包含多个字段的对象,MongoDB就会自动将它们关联起来。你只需要'$和'在一些罕见的角落案例中。即,当您需要多次使用不同的值使用另一个操作员时。 – Philipp

+0

这是在某处产生语法错误。 2016-03-15T15:37:44.046 + 0530 E QUERY SyntaxError:意外令牌: –

+0

db.test.aggregate([{$ match:{$ or:[{“$ and”:[{“name”:“pankaj” },{“test”:“false”}]},{“test”:“true”}]}}]) –