Databases and Collections

View saved

Database = namespace

A database groups collections for an app or environment (shop_dev, shop_prod). Switch with use.

use shop_dev
db

Collection = set of documents

A collection holds documents of a similar kind, such as products or orders. Documents in one collection do not need identical fields.

Create by writing

MongoDB creates the database and collection on first insert. You can also create explicitly when you need options.

db.createCollection("products")
db.products.insertOne({ sku: "TEE-1", price: 19.99 })

Drop carefully

Dropping removes data permanently. Double-check the database name first.

show collections
# db.products.drop()
# db.dropDatabase()

Comments

One comment per signed-in account. Comments are saved with this page’s URL.