db.getCollectionInfos()

Definition

  • db.getCollectionInfos(filter, nameOnly, authorizedCollections)

New in version 3.0.

Returns an array of documents with collection or view information, such as name and options, forthe current database. The results depend on the user’sprivilege. For details, see Required Access.

The db.getCollectionInfos() helper wraps thelistCollections command.

The db.getCollectionInfos() method has the followingoptional parameter:

ParameterTypeDescriptionfilterdocumentOptional. A query expression to filter the list of collections.

You can specify a query expression on any of the fieldsreturned by db.getCollectionInfos.nameOnlybooleanOptional. A flag to indicate whether the command should return just thecollection/view names and type or return both the name and other information.

Returning just the name and type (view or collection) doesnot take collection-level locks whereas returning full collectioninformation locks each collection in the database.

The default value is false.

Note

When nameOnly is true, your filter expression can onlyfilter based on a collection’s name and type. No other fields areavailable.

New in version 4.0.

authorizedCollectionsbooleanOptional. A flag, when set to true and used with nameOnly: true, thatallows a user without the required privilege (i.e.listCollections action on the database) to run thecommand when access control is enforced.

When both authorizedCollections and nameOnly options are setto true, the command returns only those collections for which the userhas privileges. For example, if a user has find actionon specific collections, the command returns only those collections; or,if a user has find or any other action, on thedatabase resource, the command lists all collections in the database.

The default value is false. That is, the user must havelistCollections action on the database to run thecommand.

For a user who has listCollections action on thedatabase, this option has no effect since the user has privileges tolist the collections in the database.

When used without nameOnly: true, this option has no effect.That is, the user must have the required privileges to run thecommand when access control is enforced. Otherwise, the user isunauthorized to run the command.

New in version 4.0.

Changed in version 3.2.

MongoDB 3.2 added support for document validation. db.getCollectionInfos()includes document validation information in the optionsdocument.

db.getCollectionInfos() does not return validationLeveland validationAction unless they are explicitly set.

Required Access

Since db.getCollectionInfos() is a wrapper around thelistCollections, users must have the same privileges aslistCollections when access control is enforced.

To run listCollections when access control is enforced,users must, in general, have privileges that grantlistCollections action on the database. For example,the following privilege grants users to rundb.getCollectionInfos() against the test database:

  1. { resource: { db: "test", collection: "" }, actions: [ "listCollections" ] }

The built-in role read provides the privilege to runlistCollection for a specific database.

Starting in version 4.0, however, user without the required privilegecan run the command with both authorizedCollections andnameOnly options set to true. In this case, the command returnsjust the name and type of the collection(s) to which the user hasprivileges.

For example, consider a user with a role that grants just the followingprivilege:

  1. { resource: { db: "test", collection: "foo" }, actions: [ "find" ] }

The user can run the command if the command includes bothauthorizedCollections and nameOnly options set to true(with or without the filter option):

  1. db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )

The operation returns the name and type of the foo collection.

However, the following operations (with or without the filteroption) error for the user without the required access:

  1. db.runCommand( { listCollections: 1.0, authorizedCollections: true } )
  2. db.runCommand( { listCollections: 1.0, nameOnly: true } )

show collections

Starting in version 4.0 of the mongo shell, showcollections is equivalent to:

  1. db.runCommand( { listCollections: 1.0, authorizedCollections: true, nameOnly: true } )
  • For users with the required access, show collections lists thenon-system collections for the database.
  • For users without the required access, show collections listsonly the collections for which the users has privileges.

Earlier MongoDB Versions

When a version 4.0 mongo shell is connected to anearlier version MongoDB deployment that does not supportauthorizedCollections and nameOnly options,

  • A user must have the required access to runlistCollection.
  • If a user does not have required access and runs showcollections, MongoDB uses theauthenticatedUserPrivileges fieldreturned by connectionStatus to return an approximatelist of collections for the user.

Behavior

Client Disconnection

Starting in MongoDB 4.2, if the client that issued the db.getCollectionInfos()disconnects before the operation completes, MongoDB marksthe db.getCollectionInfos() for termination (i.e. killOp on theoperation).

Example

The following returns information for all collections in theexample database:

  1. use example
  2. db.getCollectionInfos()

The method returns an array of documents that contain collectioninformation:

  1. [
  2. {
  3. "name" : "employees",
  4. "type" : "collection",
  5. "options" : {
  6. "flags" : 1,
  7. "validator" : {
  8. "$or" : [
  9. {
  10. "phone" : {
  11. "$exists" : true
  12. }
  13. },
  14. {
  15. "email" : {
  16. "$exists" : true
  17. }
  18. }
  19. ]
  20. }
  21. },
  22. "info" : {
  23. "readOnly" : false,
  24. "uuid" : UUID("222e18ca-4a10-4a42-a8fe-c39255cc4c55")
  25. },
  26. "idIndex" : {
  27. "v" : 2,
  28. "key" : {
  29. "_id" : 1
  30. },
  31. "name" : "_id_",
  32. "ns" : "example.employees"
  33. }
  34. },
  35. {
  36. "name" : "products",
  37. "type" : "collection",
  38. "options" : {
  39. "flags" : 1
  40. },
  41. "info" : {
  42. "readOnly" : false,
  43. "uuid" : UUID("1bc898b2-3b91-45e4-9d8b-0be462d5a157")
  44. },
  45. "idIndex" : {
  46. "v" : 2,
  47. "key" : {
  48. "_id" : 1
  49. },
  50. "name" : "_id_",
  51. "ns" : "example.products"
  52. }
  53. },
  54. {
  55. "name" : "mylogs",
  56. "type" : "collection",
  57. "options" : {
  58. "capped" : true,
  59. "size" : 256
  60. },
  61. "info" : {
  62. "readOnly" : true,
  63. "uuid" : UUID("8e62116d-b6a0-490a-808c-258ccb7ea947")
  64. },
  65. "idIndex" : {
  66. "v" : 2,
  67. "key" : {
  68. "_id" : 1
  69. },
  70. "name" : "_id_",
  71. "ns" : "example.mylogs"
  72. }
  73. }
  74. ]

To request collection information for a specific collection,specify the collection name when calling the method, as in the following:

  1. use example
  2. db.getCollectionInfos( { name: "employees" } )

The method returns an array with a single document that details thecollection information for the employees collection in theexample database.

  1. [
  2. {
  3. "name" : "employees",
  4. "type" : "collection",
  5. "options" : {
  6. "flags" : 1,
  7. "validator" : {
  8. "$or" : [
  9. {
  10. "phone" : {
  11. "$exists" : true
  12. }
  13. },
  14. {
  15. "email" : {
  16. "$exists" : true
  17. }
  18. }
  19. ]
  20. }
  21. },
  22. "info" : {
  23. "readOnly" : false,
  24. "uuid" : UUID("222e18ca-4a10-4a42-a8fe-c39255cc4c55")
  25. },
  26. "idIndex" : {
  27. "v" : 2,
  28. "key" : {
  29. "_id" : 1
  30. },
  31. "name" : "_id_",
  32. "ns" : "example.employees"
  33. }
  34. }
  35. ]

You can specify a filter on any of the fields returned bygetCollectionInfos.

For example, the following command returns information for allcollections in the example database where info.readOnly istrue:

  1. use example
  2. db.getCollectionInfos( { "info.readOnly" : true } )

The command returns the following:

  1. [
  2. {
  3. "name" : "mylogs",
  4. "type" : "collection",
  5. "options" : {
  6. "capped" : true,
  7. "size" : 256
  8. },
  9. "info" : {
  10. "readOnly" : true,
  11. "uuid" : UUID("8e62116d-b6a0-490a-808c-258ccb7ea947")
  12. },
  13. "idIndex" : {
  14. "v" : 2,
  15. "key" : {
  16. "_id" : 1
  17. },
  18. "name" : "_id_",
  19. "ns" : "example.mylogs"
  20. }
  21. }
  22. ]