如何计算 MongoDB 返回的文档数

How to get count of documents returned by MongoDB

我想获取 MongoDB 返回的文档数。

下面是 returns 一些文件的命令,但我想不出一种方法来计算文件的数量。

db.runCommand( { 
  usersInfo: 1, 
  filter: { 
     roles: { $in: [
        { role: "root", db: "admin" },
        { role: "clusterAdmin", db: "admin" },
        { role: "clusterManager", db: "admin" },
        { role: "clusterMonitor", db: "admin" },
        { role: "hostManager", db: "admin" },
        { role: "backup", db: "admin" },
        { role: "readAnyDatabase", db: "admin" },
        { role: "readWriteAnyDatabase", db: "admin" },
        { role: "userAdminAnyDatabase", db: "admin" },
        { role: "dbAdminAnyDatabase", db: "admin" }
     ] }  
    } 
} ).users

有人可以解释一下吗。

由于命令return一个数组,最简单的就是查看数组的长度代表文件的个数:

db.runCommand( { usersInfo: 1, filter: { roles: { $in: [{ role: "root", db: "admin" },{ role: "clusterAdmin", db: "admin" },{ role: "clusterManager", db: "admin" },{ role: "clusterMonitor", db: "admin" },{ role: "hostManager", db: "admin" },{ role: "backup", db: "admin" },{ role: "readAnyDatabase", db: "admin" },{ role: "readWriteAnyDatabase", db: "admin" },{ role: "userAdminAnyDatabase", db: "admin" },{ role: "dbAdminAnyDatabase", db: "admin" }] } } } ).users.length