Nellonidas
Nellonidas

Reputation: 183

How to get the highest value of each date in a not finding MongoDb?

In this case, the problem I'm having is to make a distinction of a list taking all the values of each registered day. Example:

{car: "Volvo", km:"101", dateInsert: : ISODate("2019-10-22T14:43:32.924+01:00")},

{car: "Volvo", km:"105", dateInsert: : ISODate("2019-10-22T14:50:32.924+01:00")},

{car: "Volvo", km:"115", dateInsert: : ISODate("2019-10-23T19:43:32.924+01:00")},

{car: "Volvo", km:"119", dateInsert: : ISODate("2019-10-23T20:30:32.924+01:00")},

{car: "BMW", km:"40", dateInsert: : ISODate("2019-10-10T14:43:32.924+01:00")},

{car: "BMW", km:"43", dateInsert: : ISODate("2019-10-10T14:50:32.924+01:00")},

{car: "BMW", km:"50", dateInsert: : ISODate("2019-10-10T19:43:32.924+01:00")},

{car: "BMW", km:"59", dateInsert: : ISODate("2019-10-11T20:30:32.924+01:00")}, ...

In this case is on bold showing only I need the last record of each day.

Whats is good way to get this records?

Upvotes: 0

Views: 55

Answers (2)

Takis
Takis

Reputation: 8705

Test code here

MongoDB can compare documents also, by comparing the fields one by one, if they have same name, so putting km first field, we will get the max km first. $$ROOT is a system variable, has the value for all the document.

db.collection.aggregate([
  {
    "$group": {
      "_id": {
        "year": {
          "$year": "$dateInsert"
        },
        "day": {
          "$dayOfYear": "$dateInsert"
        }
      },
      "max": {
        "$max": {
          "km": "$km",
          "doc": "$$ROOT"
        }
      }
    }
  },
  {
    "$replaceRoot": {
      "newRoot": "$max.doc"
    }
  }
])

Upvotes: 2

NeNaD
NeNaD

Reputation: 20354

  • $substr and $dateToString to extract only the date from the ISODate.
  • $group and $addToSet to group all documents by the unique date.
  • $reduce to find the document with maximum value of km field for each date.
  • $project to project the result with proper fields
db.collection.aggregate([
  {
    "$set": {
      "dateInsert": {
        $substr: [
          {
            $dateToString: {
              date: "$dateInsert"
            }
          },
          0,
          10
        ]
      }
    }
  },
  {
    "$group": {
      "_id": "$dateInsert",
      "results": {
        $addToSet: {
          car: "$car",
          km: "$km"
        }
      }
    }
  },
  {
    "$set": {
      "results": {
        $reduce: {
          input: "$results",
          initialValue: {
            km: 0
          },
          in: {
            $cond: [
              {
                $gte: [
                  "$$this.km",
                  "$$value.km"
                ]
              },
              "$$this",
              "$$value"
            ]
          }
        }
      }
    }
  },
  {
    "$project": {
      dateInsert: "$_id",
      car: "$results.car",
      km: "$results.km",
      _id: 0
    }
  }
])

Here is the working example: https://mongoplayground.net/p/NkBxmdFT1_F

Upvotes: 1

Related Questions