2014-03-05 2 views
0

자식 배열에 특정 값을 포함하는 문서를 찾고 싶습니다.Mongodb 집계 배열 항목과 자식 배열 항목이 일치합니다.

{ 
"_id" : ObjectId("52e9658e2a13df5be22cf7dc"), 
"desc" : "Something somethingson", 
"imageurl" : "http://", 
"tags" : [ 
    { 
     "y" : 29.3, 
     "brand" : "52d2cecd0bd1bd844d000018", 
     "brandname" : "Zara", 
     "type" : "Bow Tie", 
     "x" : 20, 
     "color" : "52d50c19f8f8ca8448000001", 
     "number" : 0, 
     "season" : 0, 
     "cloth" : "52d50d57f8f8ca8448000006" 
    }, 
    { 
     "y" : 29.3, 
     "brand" : "52d2cecd0bd1bd844d000018", 
     "brandname" : "Zara", 
     "type" : "Bow Tie", 
     "x" : 20, 
     "color" : "52d50c19f8f8ca8448000001", 
     "number" : 0, 
     "season" : 0, 
     "cloth" : "52d50d57f8f8ca8448000006" 
    } 
], 
"user_id" : "52e953942a13df5be22cf7af", 
"username" : "Thompson", 
"created" : 1386710259971, 
"occasion" : "ID", 
"sex" : 0 
} 

나는 이런 식으로 뭔가 보여야 할 싶습니다 쿼리 :

db.posts.aggregate([ 
     {$match: {tags.color:"52d50c19f8f8ca8448000001", tags.brand:"52d2cecd0bd1bd844d000018", occasion: "ID"}}, 
     {$sort:{"created":-1}}, 
     {$skip:0}, 
     {$limit:10} 
    ]) 

내 문제는 내가 내부에 아무것도 일치하는 방법을 알고 해달라고입니다

는 예를 들어 문서입니다 "tags"와 같은 문서의 배열. 어떻게해야합니까?

답변

2

당신은 통합 프레임 워크없이 그것을 수행하려고 할 수 있습니다 :

db.posts.find(
{ 
    occasion: "ID", 
    tags: { $elemMatch: { color:"52d50c19f8f8ca8448000001", brand:"52d2cecd0bd1bd844d000018" } } 
} 
).sort({created: -1}).limit(10) 

그리고 당신은 집계 사용하려면 :

db.posts.aggregate([ 
    {$match: 
     { 
      tags: { $elemMatch: { color:"52d50c19f8f8ca8448000001", brand: "52d2cecd0bd1bd844d000018" } }, 
      occasion: "ID" 
     } 
    }, 
    {$sort:{"created":-1}}, 
    {$limit:10} 
])