MongoDB查询以基于匹配条件对记录进行计数
要根据匹配条件对记录进行计数,请使用count()
。让我们创建一个包含文档的集合-
> db.demo205.insertOne(... {
...
... "id": "101",
... "Name": "",
... "Age": "",
... "isActive": false
... }
...);
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3d8a3003d395bdc21346eb")
}
> db.demo205.insertOne(
... {
...
... "id": "102",
... "Name": "Chris",
... "Age": "25",
... "isActive": true
... }
...);
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3d8a3003d395bdc21346ec")
}
> db.demo205.insertOne(
... {
...
... "id": "103",
... "Name": "",
... "Age": "",
... "isActive": false
... }
...);
{
"acknowledged" : true,
"insertedId" : ObjectId("5e3d8a3003d395bdc21346ed")
}
在find()
方法的帮助下显示集合中的所有文档-
> db.demo205.find();
这将产生以下输出-
{ "_id" : ObjectId("5e3d8a3003d395bdc21346eb"), "id" : "101", "Name" : "", "Age" : "", "isActive" : false }{ "_id" : ObjectId("5e3d8a3003d395bdc21346ec"), "id" : "102", "Name" : "Chris", "Age" : "25", "isActive" : true }
{ "_id" : ObjectId("5e3d8a3003d395bdc21346ed"), "id" : "103", "Name" : "", "Age" : "", "isActive" : false }
以下是根据匹配条件对记录进行计数的查询-
> db.demo205.count({... Name: "",
... Age: "",
... "isActive": false
... });
这将产生以下输出-
2
以上是 MongoDB查询以基于匹配条件对记录进行计数 的全部内容, 来源链接: utcz.com/z/316974.html