The way to view all databases in MongoDB is to enter the command "show dbs". 1. This command displays only non-empty databases. 2. You can switch the database through the "use" command and insert data to display it. 3. Pay attention to internal databases such as "local" and "config". 4. When using the driver, you need to use the "listDatabases()" method to obtain detailed information. 5. The "db.stats()" command can view detailed database statistics.
The way to view all databases in MongoDB is actually very simple, but it's just the tip of the iceberg. Let's explore this topic in depth and share some of my own experiences and tips when using MongoDB.
To view all databases in MongoDB, you just need to enter a simple command in the MongoDB shell:
show dbs
This command lists all available databases. But do you know? It is not enough to just know this command, we need to have a deeper understanding of why this command is so useful and some of the problems that may be encountered in actual use.
First, when you execute show dbs
, MongoDB displays all non-empty databases. This means that if you create a database but don't insert any data, it won't show up in the list. This can cause some confusion when you manage multiple projects or environments because you may think that a certain database does not exist. One way to solve this problem is to use the use
command to switch to the database you think exists and insert a record so that it will be displayed in show dbs
.
use myDatabase db.myCollection.insertOne({ test: "data" })
When using show dbs
, you may notice that some database names look weird, such as those with .
These are usually databases used internally by MongoDB, such as local
and config
. Understanding the role of these databases can help you better manage and optimize your MongoDB instances.
Another thing to note is that the show dbs
command is actually a secondary command of the MongoDB shell, not a command from the MongoDB server itself. This means that if you interact with MongoDB through a driver or API, you need to use a different approach to get the database list. For example, in MongoDB's Node.js driver, you can do this:
const MongoClient = require('mongodb').MongoClient; const url = 'mongodb://localhost:27017'; MongoClient.connect(url, function(err, client) { if (err) throw err; const adminDb = client.db().admin(); adminDb.listDatabases().then(function(dbs) { console.log(dbs.databases); client.close(); }); });
This method will return an object containing all database information, including not only the database name, but also the database size and other information, which will be more useful in some cases than show dbs
.
In actual use, I found a common misunderstanding that show dbs
will display the details of all databases. Actually, it's just a simple list. If you need more information, such as the size of the database, storage engine, etc., you need to use db.stats()
command.
use myDatabase db.stats()
This command will return detailed statistics of the current database, including data size, index size, etc., which is very useful for performance optimization and resource management.
Finally, share a tip: If you often need to view a database list of multiple MongoDB instances, you can write a simple script to automate the process. This not only saves time, but also reduces manual operation errors.
const MongoClient = require('mongodb').MongoClient; async function listDatabases(url) { const client = await MongoClient.connect(url); const adminDb = client.db().admin(); const dbs = await adminDb.listDatabases(); console.log(`Databases in ${url}:`); dbs.databases.forEach(db => console.log(db.name)); await client.close(); } const urls = ['mongodb://localhost:27017', 'mongodb://anotherhost:27017']; urls.forEach(url => listDatabases(url).catch(console.error));
This script can traverse multiple MongoDB instances and list the databases in each instance, which is very convenient.
Overall, show dbs
is a very basic but useful command, but to really master the use of MongoDB, you need to know more details and techniques. Hope these sharing can help you better use MongoDB.
The above is the detailed content of How to view all databases in MongoDB. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

As the market conditions pick up, more and more smart investors have begun to quietly increase their positions in the currency circle. Many people are wondering what makes them take decisively when most people wait and see? This article will analyze current trends through on-chain data to help readers understand the logic of smart funds, so as to better grasp the next round of potential wealth growth opportunities.

Recently, Bitcoin hit a new high, Dogecoin ushered in a strong rebound and the market was hot. Next, we will analyze the market drivers and technical aspects to determine whether Ethereum still has opportunities to follow the rise.

The pattern in the public chain field shows a trend of "one super, many strong ones, and a hundred flowers blooming". Ethereum is still leading with its ecological moat, while Solana, Avalanche and others are challenging performance. Meanwhile, Polkadot, Cosmos, which focuses on interoperability, and Chainlink, which is a critical infrastructure, form a future picture of multiple chains coexisting. For users and developers, choosing which platform is no longer a single choice, but requires a trade-off between performance, cost, security and ecological maturity based on specific needs.

Stablecoins are cryptocurrencies that are pegged to assets such as the US dollar and aim to maintain stable value. They are mainly divided into three types: fiat currency collateral, cryptocurrency collateral and algorithms. 1. Fiat currency collateral types such as USDT and USCD are supported by US dollar reserves; 2. Cryptocurrency collateral types such as DAI need to over-collateralize other currencies; 3. Algorithm relies on smart contracts to adjust supply but have high risks. The reasons why it is hotly discussed on platforms such as Douyin include: as a hedging tool when the crypto market falls, a bridge for novices to enter the crypto world, a way to obtain high-yield financial management in DeFi, and the application of low-cost cross-border payments. To obtain stablecoins, you can trade through mainstream exchanges such as Binance, Ouyi, and Huobi.

Cardano's Alonzo hard fork upgrade has successfully transformed Cardano from a value transfer network to a fully functional smart contract platform by introducing the Plutus smart contract platform. 1. Plutus is based on Haskell language, with powerful functionality, enhanced security and predictable cost model; 2. After the upgrade, dApps deployment is accelerated, the developer community is expanded, and the DeFi and NFT ecosystems are developing rapidly; 3. Looking ahead to 2025, the Cardano ecosystem will be more mature and diverse. Combined with the improvement of scalability in the Basho era, the enhancement of cross-chain interoperability, the evolution of decentralized governance in the Voltaire era, and the promotion of mainstream adoption by enterprise-level applications, Cardano has

Stablecoins are cryptocurrencies with value linked to stable assets. The main types include fiat currency collateral, cryptocurrency collateral and algorithmic. The three mainstream stablecoins are Tether (USDT), USD Coin (USDC) and Dai (DAI). 1. USDT is issued by Tether, with the largest market value and the strongest liquidity, and is widely used in major exchanges; 2. USDC is issued by the Centre Alliance, known for its transparency and compliance, and has gained high trust from institutions; 3. DAI is managed by MakerDAO and is a representative of decentralized stablecoins. It maintains price stability through excessive collateral crypto assets, and is highly favored by DeFi users.

The five most valuable stablecoins in 2025 are Tether (USDT), USD Coin (USDC), Dai (DAI), First Digital USD (FDUSD) and TrueUSD (TUSD).

Stablecoins are crypto assets that maintain price stability by anchoring fiat currencies such as the US dollar. They are mainly divided into three categories: fiat currency collateral, crypto asset collateral and algorithmic stablecoins. 1. USDT is issued by Tether and is the stablecoin with the largest market value and the highest liquidity. 2. USDC is released by the Centre alliance launched by Circle and Coinbase, and is known for its transparency and compliance. 3. DAI is generated by MakerDAO through over-collateralization of crypto assets and is the core currency in the DeFi field. 4. BUSD was launched in partnership with Paxos, and is regulated by the United States but has been discontinued. 5. TUSD achieves high transparency reserve verification through third-party escrow accounts. Users can use centralized exchanges such as Binance, Ouyi, and Huobi
