On this page
db.getCollectionInfos()
On this page
Definition
db.
getCollectionInfos
( filter )-
New in version 3.0.
Returns an array of documents with collection or view information, such as name and options, for the current database.
The
db.getCollectionInfos()
helper wraps thelistCollections
command.The
db.getCollectionInfos()
method has the following optional parameter:Parameter Type Description filter
document Optional. A query expression to filter the list of collections.
You can specify a query expression on any of the fields returned by
db.getCollectionInfos
.Changed in version 3.2.
MongoDB 3.2 added support for document validation.
db.getCollectionInfos()
includes document validation information in theoptions
document.db.getCollectionInfos()
does not returnvalidationLevel
andvalidationAction
unless they are explicitly set.
Example
The following returns information for all collections in the example
database:
use example
db.getCollectionInfos()
The method returns an array of documents that contain collection information:
[
{
"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"
}
}
]
To request collection information for a specific collection, specify the collection name when calling the method, as in the following:
use example
db.getCollectionInfos( { name: "employees" } )
The method returns an array with a single document that details the collection information for the employees
collection in the example
database.
[
{
"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"
}
}
]
You can specify a filter on any of the fields returned by getCollectionInfos
.
For example, the following command returns information for all collections in the example
database where info.readOnly
is true
:
use example
db.getCollectionInfos( { "info.readOnly" : true } )
The command returns the following:
[
{
"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"
}
}
]