Mongodb Node.js获取插入文档的数量
我想获得插入logging的计数,而不必重复调用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()函数接受一个insertWriteOpCallback和一个insertWriteOpResult。
所以有一个属性insertedCount
是插入文档的总量。 将.count()
replace为.insertedCount
。
资料来源: http : //mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#~insertWriteOpResult