簡體   English   中英

Mongodb Node.js獲取插入文檔的數量

[英]Mongodb Node.js Get count of inserted documents

我想獲取插入記錄的數量,而無需重復調用db.collection.count()

我的密碼

exports.save = function(json) {
    var url = 'mongodb://localhost/apps';
    MongoClient.connect(url, function(err, db) {
      var collection = db.collection("apps");
      collection.insert(json, {w: 1}, function(err, records){
        console.log("Inserted : "+records.count() );
      });
    });
};

TypeError:無法讀取未定義的屬性“ count”

insert()函數接受帶有insertWriteOpResult的insertWriteOpCallback。

因此,有一個屬性insertedCount ,它是插入的文檔總數。 .insertedCount替換.count()

來源: http : //mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#~insertWriteOpResult

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM