Im wondering if the following is possible in MongoDB.
I have collection of documents of following shape:
{
"day" : ISODate("2018-12-31T23:00:00.000Z"),
"value": [some integer value]
}
Is it possible to query this collection to get only documents that has different value than previous one (when sorting by day asc)? For example, having following documents:
{
"day" : ISODate("2019-04-01T00:00:00.000Z"),
"value": 10
},
{
"day" : ISODate("2019-04-02T00:00:00.000Z"),
"value": 10
},
{
"day" : ISODate("2019-04-03T00:00:00.000Z"),
"value": 15
},
{
"day" : ISODate("2019-04-04T00:00:00.000Z"),
"value": 15
},
{
"day" : ISODate("2019-04-05T00:00:00.000Z"),
"value": 15
},
{
"day" : ISODate("2019-04-06T00:00:00.000Z"),
"value": 10
}
I want to retrieve documents for 2018-04-01, 2018-04-03 and 2018-04-06. There are no 'holes' in the data, I have entries for all days within some period.
---- EDIT
I dont see how linked answers can also answer my problem. I can't group by value because value can repeat later and I need to have all periods in which value was equal to some number. In my above example you can change value for 2018-04-06 to 10 and I still have to get three documents as a result.
---- EDIT 2
Results I've received after running
aggregate([{ "$sort": { "day": 1, "value": 1 } }, { "$group": { "_id": "$value", "day": { "$first": "$day" } } }])
are as this:
/* 1 */
{
"_id" : 15.0,
"day" : ISODate("2019-04-03T00:00:00.000Z")
}
/* 2 */
{
"_id" : 10.0,
"day" : ISODate("2019-04-01T00:00:00.000Z")
}
which is NOT what Im looking for (There should be three documents, two with value 10).