0

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).

xersiee
  • 4,432
  • 1
  • 14
  • 23
  • You want [`$first`](https://docs.mongodb.com/manual/reference/operator/aggregation/first/) which is commonly used with [`$sort`](https://docs.mongodb.com/manual/reference/operator/aggregation/first/). See the linked answer, but also generally: `aggregate([{ "$sort": { "day": 1, "value": 1 } }, { "$group": { "_id": "$value", "day": { "$first": "$day" } } }])`. You can also get a whole document with `"$first": "$$ROOT"` if you had more data than just these two fields. – Neil Lunn Apr 09 '19 at 13:02
  • I've edited my question to show that this is not the case when simple usage of `$first` and `$sort` will do the trick. – xersiee Apr 09 '19 at 13:07
  • The only edit I see is *"i don't see how linked answers..."* which is basically the same response I have seen from 100's of marked duplicates before. You just got notified 5 minutes ago. You have not read it nor tested it. If you did you would see the result you asked for is what that returns. You are not the first to ask the question ( no pun intended ). If you think it does not work for you, then edit your question to show you ran the code as shown in the comment and how the result is not the same as your expected output. But you will find it's the same as what you asked for. – Neil Lunn Apr 09 '19 at 13:11
  • 1
    5 minutes is enough to see that simple grouping by value is not the solution here. Anyway 'thanks' for marking it as duplicate so I'm losing chances to get correct answer. – xersiee Apr 09 '19 at 13:26
  • @xersiee, it's not a dupe. Reopening bureaucracy is quite slow but if you ask again I'll post the answer. – Alex Blex Apr 09 '19 at 15:42
  • @AlexBlex I've created new one and rephrased title a bit: https://stackoverflow.com/questions/55606445/query-mongo-to-detect-value-changes-in-time-series – xersiee Apr 10 '19 at 06:56

0 Answers0