如何获取 MongoDB 集合中文件的数量?
以下是获取 MongoDB 集合中文件数量的语法
let anyVariableName= db.getCollection(‘yourCollectionName’); yourVariableName.count();
让我们首先创建一个包含文件的集合
> db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Bob"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a5e2015e86fd1496b38a1")
}
>db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Ramit","CustomerAge":23});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a5e3515e86fd1496b38a2")
}
>db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Adam","CustomerAge":27,"CustomerCountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a5e4c15e86fd1496b38a3")
}以下是使用 find() 方法显示某个集中所有文件的查询
> db.countNumberOfDocumentsDemo.find().pretty();
这将产生以下输出
{ "_id" : ObjectId("5c9a5e2015e86fd1496b38a1"), "CustomerName" : "Bob" }
{
"_id" : ObjectId("5c9a5e3515e86fd1496b38a2"),
"CustomerName" : "Ramit",
"CustomerAge" : 23
}
{
"_id" : ObjectId("5c9a5e4c15e86fd1496b38a3"),
"CustomerName" : "Adam",
"CustomerAge" : 27,
"CustomerCountryName" : "US"
}以下是获取 MongoDB 集合中文件数量的查询,
> let myCollectionName = db.getCollection('countNumberOfDocumentsDemo');
> myCollectionName.count();这将产生以下输出
3
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP