TExcelTAD
Home › MongoDB › MongoDB Create Database

MongoDB Create Database

One MongoDB server can host many databases, and each database holds its own collections. There is no CREATE DATABASE command: a database is created automatically the first time you store data in it.

Switching databases

In the interactive shell you switch with use:

text
test> use shop
switched to db shop
shop>

The prompt changes, and db now points at shop. But shop doesn't really exist yet: it appears in show dbs only after something is written into it.

Inside a script, use shop is replaced by a method call. db.getSiblingDB(name) returns another database on the same server without changing db, and the helper use('shop') switches db like the shell command does:

mongosh · try it yourself
const blog = db.getSiblingDB('blog_demo');
print('name:', blog.getName());
print('collections before:', blog.getCollectionNames());

blog.posts.insertOne({ _id: 1, title: 'Hello, documents!' });
print('collections after:', blog.getCollectionNames());
print(blog.posts.findOne());

blog.dropDatabase();
name: blog_demo
collections before: []
collections after: [ 'posts' ]
{ _id: 1, title: 'Hello, documents!' }

Writing the first document created both the database blog_demo and its collection posts.

Does a database exist?

show dbs lists databases interactively. Its script equivalent is the listDatabases admin command. Only databases with data are listed:

mongosh · try it yourself
const exists = (name) =>
  db.adminCommand({ listDatabases: 1, nameOnly: true }).databases.some(d => d.name === name);

const inventory = db.getSiblingDB('inventory_demo');
print('before insert:', exists('inventory_demo'));
inventory.items.insertOne({ _id: 'pen', qty: 10 });
print('after insert: ', exists('inventory_demo'));
inventory.dropDatabase();
print('after drop:   ', exists('inventory_demo'));
before insert: false
after insert:  true
after drop:    false

Deleting a database

db.dropDatabase() deletes the current database with all its collections and indexes. It cannot be undone, so double-check db.getName() first:

mongosh · try it yourself
const tmp = db.getSiblingDB('scratch_demo');
tmp.a.insertOne({ _id: 1 });
tmp.b.insertOne({ _id: 1 });
print(tmp.dropDatabase());
print(tmp.getCollectionNames());
{ ok: 1, dropped: 'scratch_demo' }
[]

Naming rules

Rule Example
Case-sensitive, but two names differing only in case can't coexist Shop and shop conflict
No /, \, ., ", $, spaces or null characters (Windows also forbids *<>:? and the pipe) my.shop is invalid
At most 63 characters
Some names are reserved for the server admin, local, config

Tip: Use short lowercase names with underscores: shop, shop_test, analytics_2026.

The built-in databases

Database Purpose
admin users, roles and server-wide commands
local data that is never replicated (for example the replication log)
config internal metadata for sharded clusters and sessions

Leave these alone and keep your application's data in your own database.

Common mistakes

  • Typos create new databases. use shpo followed by an insert silently creates a database called shpo. If your data "disappeared", check the database name.
  • Expecting use to create something. use only changes which database db refers to.

Test yourself

When is a new database actually created?

  1. When you run use name
  2. When you first store data in it
  3. When you call db.createDatabase()
  4. When the server restarts

In a script, how do you get a handle on the database reports without changing db?

  1. use reports
  2. db.reports
  3. db.getSiblingDB('reports')
  4. new Database('reports')