Document database foundations

Navigate with mongosh

Connect with the current shell, select a database, inspect collections, and understand when MongoDB creates them.

9 minute lesson

~~~

The current MongoDB shell is mongosh. Connect, run db to inspect the selected database, then use use animals to switch context.

MongoDB creates a database and ordinary collection when the first document is written. You can also create a collection explicitly when you need validation, capped behavior, or other options from the start.

Create an animals database, insert one document, then list databases and collections. Remove the practice data when you finish.

Create a disposable database and inspect what MongoDB creates:

use animals
db.animals.insertOne({ name: 'Luna', age: 4 })
show collections
db.animals.find()

Run db.getName() before each destructive command. Notice that selecting animals did not persist anything until the insert. Remove the practice database only after confirming you are still connected to the local lab rather than Atlas or another shared server.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →