简体   繁体   English

在 nodejs 中检索最后一个 mongodb 条目

[英]Retrieve last mongodb entry in nodejs

I Need help to retrieve last record from my collection.我需要帮助从我的收藏中检索最后一条记录。 I have this code:我有这个代码:

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

使用此查询:

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

Check out this:看看这个:

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

read about $natural here 在这里阅读$natural

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM