I'm scraping and saving a bunch of data into a mongodb collection by pushing it into an array, an example of the saved data looks like this:
{
"_id" : ObjectId("616eafce1e7edf6e9b033938"),
"collectibleId" : "34e05c2c-19dd-4509-a98c-bb11cd275d34",
"__v" : NumberInt(0),
"createdAt" : ISODate("2021-10-19T11:45:16.845 0000"),
"history" : [
{
"storePrice" : 6.99,
"value" : NumberInt(27),
"date" : ISODate("2021-10-19T11:45:16.683 0000"),
"totalListings" : NumberInt(904),
"hourlyChange" : {
"market" : NumberInt(0)
}
},
{
"storePrice" : 6.99,
"value" : 27.5,
"date" : ISODate("2021-10-19T11:59:01.192 0000"),
"totalListings" : NumberInt(902),
"hourlyChange" : {
"market" : NumberInt(0)
}
},
... loads more items ...
],
"updatedAt" : ISODate("2021-10-27T18:59:01.109 0000")
}
I'm trying to now write a query for the data that can return only the recorded data within a specific timeframe. So far I've tried this:
Prices.findOne({
collectibleId: slug,
history: {
date: {
$gte: new Date("2021-10-26T00:00:00.000Z")
}
}
}).exec((err, data) => {
if (err){
return res.status(400).json({
error: errorHandler(err)
})
}
res.json(data)
})
But the result im getting is always null. I'm wondering if it's because date is nested in history and perhaps im writing that wrong ? Any advice appreciated thank you.
CodePudding user response:
$match
then $set
db.collection.aggregate([
{
"$match": {
collectibleId: "slug",
"history.date": {
$gte: ISODate("2021-10-26T00:00:00.000Z")
}
}
},
{
"$set": {
"history": {
"$filter": {
"input": "$history",
"as": "h",
"cond": {
$gte: [
"$$h.date",
ISODate("2021-10-26T00:00:00.000Z")
]
}
}
}
}
}
])