2015-10-15 26 views
1
//STUDENT collection 

{ 

'name':'qwerty', 

'college':'123' , 

'BLOGS':[ 

{'title':'music', 'desc':'party' } 
, 

{'title':'sports', 'desc':'cricket' } 

] 

} 

我需要打印整个文档where title ='sports'?find()mongodb文件asp内部数组元素条件

db.STUDENT.find({'BLOGS.title':'sports'},{'_id':0,'BLOGS':1}); 

不适合我,帮忙!!!!!

回答

0

使用$elemMatch只返回BLOGS阵列,其中title字段具有"sports"一个值的第一匹配部件:

db.STUDENT.find({ "BLOGS.title": "sports" }, 
    { 
     "_id": 0, 
     "BLOGS": { 
      "$elemMatch": { "title": "sports" } 
     } 
    } 
) 

样本输出:

/* 0 */ 
{ 
    "BLOGS" : [ 
     { 
      "title" : "sports", 
      "desc" : "cricket" 
     } 
    ] 
} 
+0

Thanyou“$ elemMatch“工作。 –