List all collections in MongoDB Shell
When using MongoDB , there are several ways to list the collections in the database. This article will discuss four different ways to get a list of collections in a MongoDB database.
These methods are as follows:
show collections
List all collections in MongoDB Shell
using command
You will use the Mongo shell command show collections
to get a list of MongoDB collections. This command generates a list of all the collections you have created in your MongoDB database.
It would be best if you first select a database where at least one collection exists to use the command.
Let's use use
the command to select a database.
use <BlocTAKdatabase>
Afterwards, you can use show collections
query to run commands.
show collections
The result of the above query is shown below.
employees
clients
clienttype
services
system.views
In this case, there are five results. clienttype
is a view, although you can't tell by looking at it.
The remaining items are collections. system.views
is a system collection that contains information about each database view.
Your access level will determine the output of the returned collection.
listCollections
List all collections in MongoDB Shell
using command
Administrator command listCollections
Returns the names and options of all collections and views in the database. The generated data is in the form of documents.
Given below is the query for this command.
db.runCommand( { listCollections: 1, authorizedCollections: true, nameOnly: true } )
The result of the above query is given in the screenshot below.
This document contains information on creating a cursor for a collection of information. You can also run the command like this:
db.runCommand( { listCollections: 1.0 } )
Using this can provide a wealth of information about a collection. Check out the example below db.getCollectionInfos()
to see the data returned when you run it like this.
db.getCollectionNames()
List all collections in MongoDB Shell
using method
The function db.getCollectionNames()
returns an array containing the names of all collections and views in the current database. In addition, if access control is enabled, the names of the collections are based on the user's permissions.
The query for this method is given below.
db.getCollectionNames()
The result of the above query is shown below.
[ "employees", "clients", "clienttype", "services", "system.views" ]
db.getCollectionInfos()
List all collections in MongoDB Shell
using method
db.getCollectionInfos()
The method generates an array of documents that contain information about the current set of database views, such as name and options. Again, it determines the results by the user's permission level.
Here you have a query that calls it without any parameters.
db.getCollectionInfos()
The result of the above query is given in the screenshot below.
db.getCollectionInfos()
The real definition is as follows:
db.getCollectionInfos(filter, nameOnly, authorizedCollections)
Therefore, you can use filter
the parameter to narrow the collection list by a query phrase. You can use it on any field returned by the method.
nameOnly
The parameter can specify that the method should return only the name of the collection.
nameOnly: true
When used
with the , authorizedCollections
parameter allows a user to run a command without the required permissions to apply access controls. In this case, the command returns only the collections that the user has access to.
Here is a query using with these parameters db.getCollectionInfos()
.
db.getCollectionInfos( {}, true, true )
The result of the above query is given in the screenshot below.
Here is an example of a query where you can filter it to a specific name.
db.getCollectionInfos( { name: "clients" }, true, true )
The result of the above query is shown below.
[ { "name" : "clients", "type" : "collection" } ]
This is what happens when you remove the last two parameters.
db.getCollectionInfos( { name: "clients" } )
The result of the above query is shown below.
[
{
"name" : "clients",
"type" : "collection",
"options" : {
},
"info" : {
"readOnly" : false,
"uuid" : UUID("91d1c6d8-8516-455d-a3c2-b157e1998f8c")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
}
}
]
So, using this MongoDB article, you now have four different methods at your disposal to list all the collections present in a MongoDB database. These methods are show collections
the command, listCollections
command, db.getCollectionNames()
method, and db.getCollectionInfos()
method.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Use find operator to join multiple conditions in MongoDB
Publish Date:2025/04/29 Views:132 Category:MongoDB
-
$lookup Today, we will see how to concatenate multiple conditions using the operator in MongoDB . In addition, we will explore some examples to demonstrate the use of $group the stage and $unionWidth the aggregation stage. $lookup Use the o
Date comparison in MongoDB
Publish Date:2025/04/28 Views:170 Category:MongoDB
-
This MongoDB tutorial discusses the issue of returning Date-based queries. In addition, there is also a good tutorial on how to use Date Range queries in MongoDB. Using date ranges in MongoDB We will learn to write MongoDB date range querie
Find objects between two dates in MongoDB
Publish Date:2025/04/28 Views:95 Category:MongoDB
-
In this article, the problem of finding objects between two dates was briefly discussed. In addition, the operators $gte, $lte, $gt, and $lt used for this purpose were briefly explained in detail. Querying date ranges in MongoDB This sectio
Comparing Dates in MongoDB
Publish Date:2025/04/28 Views:115 Category:MongoDB
-
Date is a common field in most databases, and sometimes we need to find exact documents from a collection in MongoDB. For example, if we have a collection of orders, we might search for those documents before or after a specific date. In th
Convert string to date in MongoDB
Publish Date:2025/04/28 Views:162 Category:MongoDB
-
MongoDB is an excellent platform that is growing in popularity. Among the various features it offers, MongoDB also allows you to convert data from one type to another. This may seem like a complex function, but it is very simple to execute.
Building the MongoDB REST API
Publish Date:2025/04/28 Views:71 Category:MongoDB
-
MongoDB is a flexible and scalable document-oriented database system that is widely used for large-volume data storage. It uses documents and collections instead of the traditional rational database approach of using tables and rows. MongoD
Using ORM with MongoDB
Publish Date:2025/04/28 Views:118 Category:MongoDB
-
MongoDB introduces a NoSQL solution for data storage and management, consisting of documents represented in JSON style. Like other database systems, MongoDB can also use ORM. In this article, we will explain the concepts of ORM and general
Locking mechanism in MongoDB
Publish Date:2025/04/28 Views:96 Category:MongoDB
-
In database management systems, locking mechanisms ensure the consistency of the entire result. For example, if some writing process is in progress on the data, a read command cannot be executed at the same time. Database resources are lock
Unique Index in MongoDB
Publish Date:2025/04/28 Views:130 Category:MongoDB
-
In this article, you'll learn about unique indexes, including what they are and how to create them in MongoDB. Additionally, the process of making a user's email unique in MongoDB is briefly described. The contents of this article are as fo