I am new to MongoDB and I am having trouble retrieving only a subset of an array of embedded documents. For instance, I have the following documents:
{
"_id": "Stock1",
"data": [{"value": 10.0, "date": "2000-01-01T00:00:00.000Z"},
{"value": 12.0, "date": "2010-01-01T00:00:00.000Z"},
{"value": 14.0, "date": "2020-01-01T00:00:00.000Z"}]
},
{
"_id": "Stock2",
"data": [{"value": 10.0, "date": "2000-01-01T00:00:00.000Z"},
{"value": 8.0, "date": "2010-01-01T00:00:00.000Z"},
{"value": 6.0, "date": "2020-01-01T00:00:00.000Z"}]
},
{
"_id": "Stock3",
"data": [{"value": 10.0, "date": "2000-01-01T00:00:00.000Z"},
{"value": 10.0, "date": "2010-01-01T00:00:00.000Z"},
{"value": 10.0, "date": "2020-01-01T00:00:00.000Z"}]
}
And I would like to retrieve data between date 2010-01-01 and 2020-01-01 (included) of "Stock1" and "Stock3", i.e. I would like to end up with this:
{
"_id": "Stock1",
"data": [{"value": 12.0, "date": "2010-01-01T00:00:00.000Z"},
{"value": 14.0, "date": "2020-01-01T00:00:00.000Z"}]
},
{
"_id": "Stock3",
"data": [{"value": 10.0, "date": "2010-01-01T00:00:00.000Z"},
{"value": 10.0, "date": "2020-01-01T00:00:00.000Z"}]
}
I have tried the find command:
{"_id": {$in: ["Stock1", "Stock3"]}, "data.date": {$gte: ISODate('2010-01-01'), $lte: ISODate('2020-01-01')}}
But I am retrieving all dates, which is undesirable.
I am aware of the aggregate command but I am unsure of how to construct the pipeline. Can someone pinpoint me on how I should proceed?
Any help would be greatly appreciated!