mongodb 如何计算mongodb集合中的文档数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26720050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count the number of documents in a mongodb collection
提问by Tinniam V. Ganesh
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.
您能否让我知道用于查找集合中所有文档的方法是什么。
Thanks Ganesh
谢谢 Ganesh
采纳答案by Rahul
If you want the number of documents in a collection, then use the count
method, which returns a Promise. Here's an example:
如果您想要集合中的文档数,请使用count
返回 Promise的方法。下面是一个例子:
let coll = db.collection('collection_name');
coll.count().then((count) => {
console.log(count);
});
This assumes you're using Mongo 3.
这假设您使用的是 Mongo 3。
回答by BatScream
Traverse to the database where your collection
resides using the command:
collection
使用以下命令遍历到您所在的数据库:
use databasename;
Then invoke the count()
function on the collection
in the database.
然后调用数据库中的count()
函数collection
。
var value = db.collection.count();
and then print(value)
or simply value
, would give you the count of documents in the collection named collection
.
然后print(value)
或者简单地说value
,会给你名为collection
.
Refer: http://docs.mongodb.org/v2.2/tutorial/getting-started-with-the-mongo-shell/
参考:http: //docs.mongodb.org/v2.2/tutorial/getting-started-with-the-mongo-shell/
回答by vijayakumarviji
Since v4.0.3you can use for better performance:
从v4.0.3 开始,您可以使用以获得更好的性能:
db.collection.countDocuments()
回答by Jitendra
Simply you can use
只需您可以使用
Model.count({email: '[email protected]'}, function (err, count) {
console.log(count);
});
回答by UdayKiran Pulipati
Execute Mongo shell commands in single line for better results.
在单行中执行 Mongo shell 命令以获得更好的结果。
To get count of all documents in mongodb
获取mongodb中所有文档的数量
var documentCount = 0; db.getCollectionNames().forEach(function(collection) { documentCount++; }); print("Available Documents count: "+ documentCount);
var documentCount = 0; db.getCollectionNames().forEach(function(collection) { documentCount++; }); print("可用文档数:"+ documentCount);
Output:Available Documents count: 9
输出:Available Documents count: 9
To get all count of document results in a collection
获取集合中所有文档结果的计数
db.getCollectionNames().forEach(function(collection) { resultCount = db[collection].count(); print("Results count for " + collection + ": "+ resultCount); });
db.getCollectionNames().forEach(function(collection) { resultCount = db[collection].count(); print("+ collection + "的结果计数:"+ resultCount); });
Output:
输出:
Results count for ADDRESS: 250
Results count for APPLICATION_DEVELOPER: 950
Results count for COUNTRY: 10
Results count for DATABASE_DEVELOPER: 1
Results count for EMPLOYEE: 4500
Results count for FULL_STACK_DEVELOPER: 2000
Results count for PHONE_NUMBER: 110
Results count for STATE: 0
Results count for QA_DEVELOPER: 100
To get all dataSize of documents in a collection
获取集合中文档的所有数据大小
db.getCollectionNames().forEach(function(collection) { size = db[collection].dataSize(); print("dataSize for " + collection + ":"+ size); });
db.getCollectionNames().forEach(function(collection) { size = db[collection].dataSize(); print("dataSize for " + collection + ":"+ size); });
Output:
输出:
dataSize for ADDRESS: 250
dataSize for APPLICATION_DEVELOPER: 950
dataSize for COUNTRY: 10
dataSize for DATABASE_DEVELOPER: 1
dataSize for EMPLOYEE: 4500
dataSize for FULL_STACK_DEVELOPER: 2000
dataSize for PHONE_NUMBER: 110
dataSize for STATE: 0
dataSize for QA_DEVELOPER: 100
回答by Abhishek Singhal
db.collection('collection-name').count()
is now deprecated.
Instead of count(), we should now use countDocuments()
and estimatedDocumentCount()
.
Here is an example:
db.collection('collection-name').count()
现在已弃用。我们现在应该使用countDocuments()
and代替 count() estimatedDocumentCount()
。下面是一个例子:
`db.collection('collection-name').countDocuments().then((docs) =>{
console.log('Number of documents are', docs);
};`
To know more read the documentation:
要了解更多信息,请阅读文档:
https://docs.mongodb.com/manual/reference/method/db.collection.countDocuments/
https://docs.mongodb.com/manual/reference/method/db.collection.countDocuments/
https://docs.mongodb.com/manual/reference/method/db.collection.estimatedDocumentCount/
https://docs.mongodb.com/manual/reference/method/db.collection.estimatedDocumentCount/
回答by Rukmini Vishnubhotla
db.collection.count(function(err,countData){
//you will get the count of number of documents in mongodb collection in the variable
countdata
});
In place of collection give your mongodb collection name
代替 collection 给你的 mongodb 集合名称
回答by Ripudaman Singh
Through MongoDB Console you can see the number of documents in a collection.
通过 MongoDB 控制台,您可以查看集合中的文档数量。
1.Go to mongoDB console and issue command "use databasename". To start the console go up to the bin folder of where MongoDB is installed and click on mongo.exe to start the mongoDB console e.g If the database is myDB then command is "use myDB"
1.转到mongoDB控制台并发出命令“use databasename”。要启动控制台,请转到安装 MongoDB 的 bin 文件夹,然后单击 mongo.exe 以启动 mongoDB 控制台,例如,如果数据库是 myDB,则命令为“使用 myDB”
2.Execute this command db.collection.count() collection is like table in RDBMS. e.g if your collection name is myCollection then the command is db.myCollection.count();
2.执行此命令 db.collection.count() 集合就像 RDBMS 中的表。例如,如果您的集合名称是 myCollection 那么命令是db.myCollection.count();
this command will print the size of the collection in the console.
此命令将在控制台中打印集合的大小。