Databases & Collections
A MongoDB server can hold many databases, each made of collections — the rough equivalent of tables, except a collection doesn't require every document to share the same shape.
Switching to a database
use switches your session to a database, creating it implicitly the first time you insert something into it:
JS mongosh
use blogdb
Output
switched to db blogdb
Note:
use blogdb alone doesn't actually create the database yet — MongoDB only creates it, for real, once you insert at least one document. Run show dbs right after a plain use and you likely won't see it listed until you've written something.Creating a collection
Collections are usually created implicitly on first insert, but you can also create one explicitly, which is useful when you want to set options up front (like a validation schema):
JS mongosh
db.createCollection("posts")
Output
{ ok: 1 }Listing what exists
JS mongosh
show dbs
Output
admin 40.00 KiB blogdb 8.00 KiB config 12.00 KiB local 72.00 KiB
JS mongosh
show collections
Output
posts
Unlike a SQL table, posts here has no defined columns — every document you insert into it can carry a different set of fields, though in practice most real applications keep documents in one collection reasonably consistent on purpose.