如何计算mongodb集合中的文档数 [英] How to count the number of documents in a mongodb collection

查看:248
本文介绍了如何计算mongodb集合中的文档数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何计算集合中文档的数量.我尝试了以下

I would like to know how to count the number of documents in a collection. I tried the follow

var value = collection.count();
&&
var value = collection.find().count()
&&
var value = collection.find().dataSize()

我总是得到未定义的方法.

I always get method undefined.

您能告诉我在集合中查找全部文档的方法吗?

Can you let me know what is the method to use to find the total documents in a collection.

谢谢 加内什

推荐答案

如果要收集集合中的文档数,请使用count方法,该方法返回Promise.这是一个示例:

If you want the number of documents in a collection, then use the count method, which returns a Promise. Here's an example:

let coll = db.collection('collection_name');
coll.count().then((count) => {
    console.log(count);
});

这假设您使用的是Mongo 3.

This assumes you're using Mongo 3.

这篇关于如何计算mongodb集合中的文档数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆