1

I have a document in a collection:

{
_id: ObjectId('5eaf27de2239a42991561b55'),
username: 'ScarVite',
userId: '1',
Id: 1,
claimed: false,
done: false,
originalMessageId: '706601433004376094',
channelId: '612965560501796864',
sentMessageId: '706601435688861717',
updated_at: ISODate('2020-05-03T20:21:50.528Z')
}

I Want to get the Document associated with the highest value of the variable Id .

I know, that i could get all documents in the collection and sort them by this value, but i believe there is a better/ more beautiful way of doing this.

ScarVite
  • 327
  • 1
  • 5
  • 19

1 Answers1

1

Thank you Roman Kliuchko for your comment, i was able to figure it out thanks to him.

dbo.collection(db_collection).find({}).sort({ Id: -1 }).toArray(function (err, result) {

result[0] is the document with the highest wishId

ScarVite
  • 327
  • 1
  • 5
  • 19