首页 > 解决方案 > 尝试使用 findOne 查询

问题描述

有人可以帮我理解我错了什么。我正在尝试获取我的 mongodb 集合中最后记录的 ID

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')
        }
    })

标签: node.jsmongodb

解决方案


经过不断的尝试,我得到了这个工作

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

推荐阅读