0

I have a requirement that, I want to select max date from MongoDB using Query;

This is what I tried:

db.getCollection('rrr.backup').find({$max:'insTime'});

Getting the following error

Error: error: {
    "$err" : "Can't canonicalize query: BadValue unknown top level operator: $max",
    "code" : 17287
}

Can anyone suggest me that, how to get the result with $max operator

Ram Kowsu
  • 711
  • 2
  • 10
  • 30
  • You can use `$max` operator with only `$group` and `$project` stage not with the find query... Instead use `sort` with `limit`... Something like this `db.getCollection('rrr.backup').find({}).sort({ insTime: -1 }).limit(1)` – Ashh Sep 19 '18 at 11:20
  • Is there anyway that, I can access directly with $max? – Ram Kowsu Sep 19 '18 at 11:26
  • Please have a look at duplicate answer – Ashh Sep 19 '18 at 11:33

0 Answers0