从nodejs中的mongodb中获取匹配条件的项的计数



我是mongodb的新手。我有一个场景,我需要检查集合和gameDate=当前日期的项目计数。我在nodejs中使用以下查询。

const count = this.games.find({gameDate:currentDate}).count();
console.log(count + "items present")

我在数据库中有1条匹配记录。但我没有得到计数,而是在控制台中得到以下错误。

function(filter, callback) {
this.op = 'count';
if (typeof filter === 'function') {
callback = filter;
filter = undefined;
}
filter = utils.toObject(filter);
if (mquery.canMerge(filter)) {
this.merge(filter);
}
if (!callback) {
return this;
}
this.exec(callback);
return this;
}

我怎么能在这里计数。

您可以尝试countDocuments函数。

games.countDocuments({gameDate:currentDate}, function (err, count) {
if (err){
console.log(err)
}else{
console.log("Total:", count)
}
});

最新更新