检索Nodejs中的最后一个MongoDB条目



我需要帮助才能从我的收藏中检索最后的记录。我有此代码:

    db.collection('bbb1collection', function(err, collection) {
    collection.find({}).sort({_id:-1}).limit(1).toArray(function(err, results) {
        path = results;
    console.log(results);
}

查看以下内容:

db.collection('bbb1collection', function(err, collection) {
  collection
    .find()
    .sort({$natural: -1})
    .limit(1)
    .next()
    .then(
      function(doc) {
        console.log(doc);
      },
      function(err) {
        console.log('Error:', err);
      }
    );
});

在此处阅读有关$natural的信息

使用此查询:

db.users.find().limit(1).sort({$natural:-1})

return new Promise(async (resolve, reject) => { db.get().collection('bbb1collection').find().sort({$natural:-1}).limit(1).next().then((res) => {
    console.log(res)
    resolve(res)
});
});

最新更新