2016-03-15 189 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」}]}}]) –