Mongodb query and return specific array elements within objects

1

Well, I need a help to query certain values in a document.

I have a collection in the mongo that follows this structure, including this is the document I can get and find: link

The find this easy I find document without problems using the criterion: "pages": 32

The problem I have is to display only the array items that are in objects, so this is what I expect to return from the query for "pages": 32:

{
"book": [
 {
  "pages": 32,
  "title": "a"
 },
 {
  "pages": 32,
  "title": "c"
 },
 {
  "pages": 32,
  "title": "d"
 }
]
}

Can anyone give me the path to stones, tips or suggestions?

    
asked by anonymous 06.01.2016 / 14:10

1 answer

1

You can use Unwind's aggregate to break the array so you can group and filter.

db.'collection_name'.aggregate([
{ $unwind : "$students" },
{ $unwind : "$students.class" },
{ $unwind : "$students.class.book" },
{$match:{"students.class.book.pages":{$in:[32]}}},
{$group:
{
_id:'$students.name',
teste:{$push:'$students.class.book'}
}
}
])

$ redact can also be used

    
07.01.2016 / 15:30