基于匹配条件对记录进行计数的 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
广告