Fix This Page
Navigation

db.getCollectionInfos()

On this page

Definition

db.getCollectionInfos()

New in version 3.0.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 the listCollections command.

Changed in version 3.2: MongoDB 3.2 added support for document validation. db.getCollectionInfos() includes document validation information in the options document.

db.getCollectionInfos() does not return validationLevel and validationAction 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",
      "options" : {
         "flags" : 1,
         "validator" : {
            "$or" : [
               {
                  "phone" : {
                     "$exists" : true
                  }
               },
               {
                  "email" : {
                     "$exists" : true
                  }
               }
            ]
         }
      }
   },
   {
      "name" : "products",
      "options" : {
         "flags" : 1
      }
   },
   {
      "name" : "mylogs",
      "options" : {
         "capped" : true,
         "size" : 256
      }
   },
   {
      "name" : "restaurants",
      "options" : {
         "validator" : {
            "$and" : [
               {
                  "name" : {
                     "$exists" : true
                  }
               },
               {
                  "restaurant_id" : {
                     "$exists" : true
                  }
               }
            ]
         },
         "validationLevel" : "strict",
         "validationAction" : "error"
      }
   },
   {
      "name" : "system.indexes",
      "options" : {
      }
   }
]

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: "restaurants" } )

The method returns an array with a single document that details the collection information for the restaurants collection in the example database.

[
   {
      "name" : "restaurants",
      "options" : {
         "validator" : {
            "$and" : [
               {
                  "name" : {
                     "$exists" : true
                  }
               },
               {
                  "restaurant_id" : {
                     "$exists" : true
                  }
               }
            ]
         },
         "validationLevel" : "strict",
         "validationAction" : "error"
      }
   }
]