繁体   English   中英

获取集合中所有文档的长度 mongoose

[英]get length of all documents in collection mongoose

我正在尝试统计我的收藏中有多少文档。 我有这个,但它没有返回我需要的东西,它返回了一大堆我不需要这个简单任务的不必要信息:

var estimatedDocumentCount = ServicesModel.countDocuments({});
console.log(estimatedDocumentCount)

它返回整个查询,加上它看起来像的所有嵌入参数。 我该如何正确地做到这一点?

async function countDocuments() {
  const count = await ServicesModel.countDocuments({});
  return count;
};

const count = countDocuments();

这可能是因为countDocuments是一个异步调用,而您正在同步执行它。

请遵循Mongoose 文档中提到的语法,该文档使用回调 function 来获取计数。

ServicesModel.countDocuments({}, function (err, count) {
  console.log(count);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM