报错DeprecationWarning: collection.count is deprecated, and will be removed in a future version
nodejs操作mongodb 用db.collection(collectionName).count(json)报错 如下: nodejs操作mongodb报错 (node:664) DeprecationWarning: collection.count is deprecated, and will be removed in a future version. Use collection.countDocuments or collection.estimatedDocumentCount instead
//统计数量的方法
count(collectionName,json){
return new Promise((resolve,reject)=> {
this.connect().then((db)=> {
var result = db.collection(collectionName).count(json);
result.then(function (count) {
resolve(count);
}
)
})
})
}
2 回复
DeprecationWarning: collection.count is deprecated, and will be removed in a future version. Use collection.countDocuments or collection.estimatedDocumentCount instead解决方案: mongodb中的话把
db.collection(collectionName).count(json)
改为
db.collection(collectionName).countDocuments(json);
如果是mongoose中的把
var totalNum=await this.ctx.model.Article.find({}).count();
改为
var totalNum=await this.ctx.model.Article.find({}).countDocuments();
好的感谢