I'm querying a json structure with a nested array of items. I'd like to return the entire structure but only include the items of the nest which match the query criteria.
So - sample structure is
{
parentId:1,
items: [
{
field1: 1
field2: 2
},
{
field1: 3
field2: 4
}
]
}
I would like to query with something like this ->
db.getCollection('mycollection').find({"items.field1":1, "items.field2":2})
this works but it also brings back the second child item, what I want it to actually return is this ->
{
parentId:1,
items: [
{
field1: 1
field2: 2
}
]
}
I have been able to create a query in MongoDB itself which achieves what I want ->
db.getCollection('mycollection').aggregate(
{ $unwind : "$items" },
{ $match : {
"items.field1": 1,
"items.field2": 2,
}}
)
but when trying to set this up using mgo.v2 it's proving a bit of a mare. The collection.Find method doesn't seem to like the $unwind command and can't seem to get the right syntax to get it working with the Pipe method.
Does anyone have any suggestions how this can be created? Can I just create the string and pass it for execution?