dimanche 1 mars 2015

Mongodb how can I query a subset of an array for a specific document

I want to run a query to select specific documents. Then on each document, open up an array of sub documents and run a query to filter those sub docs.


Example:



{
"_id" : ObjectID(23412351346435),
"list" : [
{date: ISODate(2015-01-12T00:00:00.000Z), name: "Jan 12"},
{date: ISODate(2015-01-13T00:00:00.000Z), name: "Jan 13"},
{date: ISODate(2015-01-14T00:00:00.000Z), name: "Jan 14"}
]
}


I'm guessing I can do something with Mongo's aggregate function. I have been able to match the documents I want, but how can I get a sub query going on the array? I tried using $elemMatch but that only returns the first item in the array that matches the date range.


To be clear when I query for ObjectID(23412351346435) and Date range 2015-01-12 to 2015-01-13 I want it to return this;



{
"_id" : ObjectID(23412351346435),
"list" : [
{date: ISODate(2015-01-12T00:00:00.000Z), name: "Jan 12"},
{date: ISODate(2015-01-13T00:00:00.000Z), name: "Jan 13"}
]
}

Aucun commentaire:

Enregistrer un commentaire