列出MongoDB数据库中集合的4种方法
使用MongoDB时,有多种方法可以列出数据库中的集合。
以下是获取MongoDB数据库中集合列表的四种方法:
- 命令
show collections - 命令
listCollections - 方法
db.getCollectionNames() - 方法
db.getCollectionInfos()
命令show collections
如果您使用的是 mongo shell,获取集合列表的最快方法是使用该命令。此命令检索当前数据库中的集合和视图的列表。show collections
例:
show collections
结果:
employees pets pettypes products system.views
在本例中,有五个结果。我们不能只看它,而实际上是一种观点。其他是集合。pettypes
该集合是一个系统集合,其中包含有关数据库中每个视图的信息。system.views
返回的实际集合将取决于您的访问级别:
- 对于具有所需访问权限的用户,列出数据库的非系统集合。
show collections - 对于没有所需访问权限的用户,仅列出用户具有权限的集合。
show collections
命令listCollections
管理命令返回数据库中集合和视图的名称和选项。它以文档的形式返回信息。listCollections
例:
db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
结果:
{ “cursor” : { “id” : NumberLong(0), “ns” : “PetHotel.$cmd.listCollections”, “firstBatch” : [ { “name” : “employees”, “type” : “collection” }, { “name” : “system.views”, “type” : “collection” }, { “name” : “pets”, “type” : “collection” }, { “name” : “products”, “type” : “collection” }, { “name” : “pettypes”, “type” : “view” } ] }, “ok” : 1 }
该文档包含用于创建集合信息的游标的信息。
这次我们可以看到哪些是集合,哪些是视图。
我们也可以像这样运行命令:
db.runCommand( { listCollections: 1.0 } )
这样做可以提供有关集合的更多信息。请参阅下面的示例以查看像这样运行时返回的数据(该方法是一个包装器)。db.getCollectionInfos()``db.getCollectionInfos()``listCollections
方法db.getCollectionNames()
该方法返回一个数组,其中包含当前数据库中所有集合和视图的名称,或者,如果使用访问控制运行,则根据用户的权限返回集合的名称。db.getCollectionNames()
例:
db.getCollectionNames()
结果:
[ “employees”, “pets”, “pettypes”, “products”, “system.views” ]
方法db.getCollectionInfos()
该方法返回一个文档数组,其中包含当前数据库的集合或视图信息,如名称和选项。结果取决于用户的权限。db.getCollectionInfos()
下面是在没有任何参数的情况下调用它的示例:
db.getCollectionInfos()
结果:
[ { “name” : “employees”, “type” : “collection”, “options” : {
},
"info" : {
"readOnly" : false,
"uuid" : UUID("07e89c25-8842-4331-a1a9-96fe0b4745dc")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
}
},
{
"name" : "pets",
"type" : "collection",
"options" : {
},
"info" : {
"readOnly" : false,
"uuid" : UUID("91d1c6d8-8516-455d-a3c2-b157e1998f8c")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
}
},
{
"name" : "pettypes",
"type" : "view",
"options" : {
"viewOn" : "pets",
"pipeline" : [
{
"$project" : {
"type" : 1
}
}
]
},
"info" : {
"readOnly" : true
}
},
{
"name" : "products",
"type" : "collection",
"options" : {
"capped" : true,
"size" : 7500544,
"max" : 7000
},
"info" : {
"readOnly" : false,
"uuid" : UUID("cb084959-f374-4f51-bbed-8998c13dcbe2")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
}
},
{
"name" : "system.views",
"type" : "collection",
"options" : {
},
"info" : {
"readOnly" : false,
"uuid" : UUID("3f458338-0678-4d0f-a0cf-eacbd43c8cad")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
}
}
]
的定义实际上是这样的:db.getCollectionInfos()
db.getCollectionInfos(filter, nameOnly, authorizedCollections)
因此,我们可以使用该参数根据查询表达式过滤集合列表。这可以应用于该方法返回的任何字段。filter
还可以使用该参数指定该方法应仅返回集合和视图的名称。nameOnly
该参数设置为 并与 一起使用时,允许没有所需权限(即对数据库的 listCollections 操作)的用户在强制实施访问控制时运行该命令。在这种情况下,该命令仅返回用户具有权限的那些集合。authorizedCollections``true``nameOnly: true
使用这些参数的示例:db.getCollectionInfos()
db.getCollectionInfos( {}, true, true )
结果:
[ { “name” : “employees”, “type” : “collection” }, { “name” : “pets”, “type” : “collection” }, { “name” : “pettypes”, “type” : “view” }, { “name” : “products”, “type” : “collection” }, { “name” : “system.views”, “type” : “collection” } ]
这是我将其过滤为特定名称的一个:
db.getCollectionInfos( { name: "pets" }, true, true )
结果:
[ { “name” : “pets”, “type” : “collection” } ]
以下是我删除最后两个参数时发生的情况:
db.getCollectionInfos( { name: "pets" } )
结果:
[ { “name” : “pets”, “type” : “collection”, “options” : {
},
"info" : {
"readOnly" : false,
"uuid" : UUID("91d1c6d8-8516-455d-a3c2-b157e1998f8c")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
}
}
]