use <db> — MongoDB Technical Interview Questions & Answers

Switches the current database context.

use <db>

  • Simple meaning: use college switches your shell to that database. If it does not exist yet, Mongo creates it on the first write.
  • How to use: use college then db.students.insertOne({...}). db.getName() shows the current DB.
  • Try this: use test then use college — watch the prompt.
  • Common mistake: dropDatabase on the wrong use. Always check db.getName() first.
  • Interview tip: use selects context. Creation is lazy until a write.

Technical Interview Tip: use + first insert creates the database.

Code Examples for use <db>

use <db> — working example

use prep_pilot
db.users.insertOne({ name: "Ada", email: "ada@example.com" })
db.getName()

use <db> switches the shell's current database; the DB is created on first write.

How to Explain use <db> to an Interviewer

Does use college create the DB immediately?

It switches context; files appear after a write