Reputation: 1800
This is my object structure in MongoDB, View JSON structure here
{
"id": 1234,
"hotel_id": 1,
"data":[
{
"date":"2019-06-21",
"price": "150",
"updated_at":"2019-05-20"
},
{
"date":"2019-06-22",
"price": "155",
"updated_at":"2019-05-20"
},
{
"date":"2019-06-23",
"price": "157",
"updated_at":"2019-05-20"
},
{
"date":"2019-06-24",
"price": "159",
"updated_at":"2019-05-21"
},
{
"date":"2019-06-25",
"price": "159",
"updated_at":"2019-05-22"
},
{
"date":"2019-06-26",
"price": "159",
"updated_at":"2019-05-23"
}
]
}
I want to filter records that have updated_at > 2019-05-21
(x date) .
Does it possible to get data between two specific dates like I want data which dates between 2019-06-21
and 2019-06-23
, don't want to get other records.
Upvotes: 0
Views: 35
Reputation: 22296
With this current design you'll have to use an aggregate, i don't really know what your product is but this just feels a little off to me, i think (if possible) you should consider re-structuring the data.
With that said i would do it like this:
db.collection.aggregate(
[
{
"$addFields" : {
"temp" : {
"$objectToArray" : "$data"
}
}
},
{
"$match" : {
"temp.v.updated_at" : {
"$gt" : "2019-05-21"
}
}
}
],
);
Edit: to count the records just add a $group stage with $sum:
{
$group: {
_id: null,
count: {$sum: 1}
}
}
Upvotes: 1