On this page
db.getCollectionInfos()
在本页面
Definition
db.
getCollectionInfos
(* filter *)- 3.0 版中的新功能。
返回具有当前数据库的集合或view信息(例如名称和选项)的文档数组。
db.getCollectionInfos()辅助程序包装listCollections命令。
db.getCollectionInfos()方法具有以下可选参数:
Parameter | Type | Description |
---|---|---|
filter |
document | 可选的。查询表达式以过滤集合列表。 |
您可以在db.getCollectionInfos上的fields returned上指定查询表达式。
在版本 3.2 中更改。
MongoDB 3.2 添加了对document validation的支持。 db.getCollectionInfos()在options
文档中包含文档验证信息。
除非明确设置,db.getCollectionInfos()不会返回validationLevel
和validationAction
。
Example
以下内容返回example
数据库中所有集合的信息:
use example
db.getCollectionInfos()
该方法返回包含集合信息的文档数组:
[
{
"name" : "employees",
"type" : "collection",
"options" : {
"flags" : 1,
"validator" : {
"$or" : [
{
"phone" : {
"$exists" : true
}
},
{
"email" : {
"$exists" : true
}
}
]
}
},
"info" : {
"readOnly" : false,
"uuid" : UUID("222e18ca-4a10-4a42-a8fe-c39255cc4c55")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.employees"
}
},
{
"name" : "products",
"type" : "collection",
"options" : {
"flags" : 1
},
"info" : {
"readOnly" : false,
"uuid" : UUID("1bc898b2-3b91-45e4-9d8b-0be462d5a157")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.products"
}
},
{
"name" : "mylogs",
"type" : "collection",
"options" : {
"capped" : true,
"size" : 256
},
"info" : {
"readOnly" : true,
"uuid" : UUID("8e62116d-b6a0-490a-808c-258ccb7ea947")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.mylogs"
}
}
]
要请求“特定”集合的集合信息,请在调用方法时指定集合名称,如下所示:
use example
db.getCollectionInfos( { name: "employees" } )
该方法返回一个包含单个文档的数组,该文档详细说明example
数据库中employees
集合的集合信息。
[
{
"name" : "employees",
"type" : "collection",
"options" : {
"flags" : 1,
"validator" : {
"$or" : [
{
"phone" : {
"$exists" : true
}
},
{
"email" : {
"$exists" : true
}
}
]
}
},
"info" : {
"readOnly" : false,
"uuid" : UUID("222e18ca-4a10-4a42-a8fe-c39255cc4c55")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.employees"
}
}
]
您可以在getCollectionInfos
返回的任何字段上指定过滤器。
例如,以下命令返回example
数据库中info.readOnly
为true
的所有集合的信息:
use example
db.getCollectionInfos( { "info.readOnly" : true } )
该命令返回以下内容:
[
{
"name" : "mylogs",
"type" : "collection",
"options" : {
"capped" : true,
"size" : 256
},
"info" : {
"readOnly" : true,
"uuid" : UUID("8e62116d-b6a0-490a-808c-258ccb7ea947")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "example.mylogs"
}
}
]