0

Can someone help me understand what I'm dong wrong. I'm trying to fetch the last recorded ID in my mongodb collection

db.collection('task').findOne(({_id: new ObjectID}).sort('_id:: -1').limit(1), (error, task) => {
        if(error){
            return console.log('There is no data inside the collection')
        }
    })
hippe_paradox
  • 31
  • 1
  • 6

1 Answers1

0

After constant tries I got this to work

db.collection('task').findOne({}, {sort: {id: -1},limit: 1}, (error, task) => {
        if(error){
            return console.log('There is no data inside the collection')
        }
        console.log(task)
    })
hippe_paradox
  • 31
  • 1
  • 6