I'm fighting with MongoDb
I have a collection of documents with single document structured as follows
{
"_id": { "$oid": "588a931d5c98fe0f3f84d93f" },
"name": "Michele",
"type": "F",
"category": "G",
"meta":{
"code": "113835667",
"updated": {"$date": "2017-02-07T00:00:00.000Z"},
"since": {"$date": "2013-11-07T23:00:00.000Z"}
},
"data": [
{
"date": {"$date": "2013-11-07T23:00:00.000Z"},
"close": 12.23
}
... // removed
{
"date": {"$date": "2017-02-07T00:00:00.000Z"},
"close": 15.22
}
]
}
What i need to achieve is returning the document with matching _id but filter out from the data array the documents with date property not inside a specified time range.
this is what I've attempted since now
let id; //[DocumentId]
let from; //[Date]
let to; //[Date]
collection.aggregate([
{ $match: { _id: { $eq: id } } },
{ $unwind: '$data' },
{ $match: { 'data.date': { $gte: from, $lte: to } } },
{ $group: { _id: '$_id',
data: { $push: { date:'$data.date', close: '$data.close' } } } }
], ...);
the problem with this approach is that the document I return only contains _id and data properties [data filtering result is ok] while I need to return the full set of available properties.
suggestions are greatly appreciated!