簡體   English   中英

在 nodejs 中檢索最后一個 mongodb 條目

[英]Retrieve last mongodb entry in nodejs

我需要幫助從我的收藏中檢索最后一條記錄。 我有這個代碼:

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

看看這個:

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

 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