Creating a Database — MongoDB Technical Interview Questions & Answers
Databases are created implicitly on first write.
Creating a Database
- Simple meaning: You do not CREATE DATABASE like SQL. use college then write a document — the database appears.
- How to use: use college db.students.insertOne({ name: "Asha", roll: 12, city: "Pune", marks: 72 }) show dbs
- Atlas: the database name is also in the connection string path, but a write still creates collections.
- Common mistake: waiting for a CREATE DATABASE command that does not exist in mongosh.
- Interview tip: “Lazy creation on first write.”
Technical Interview Tip: First insert creates the database.
Code Examples for Creating a Database
Creating a Database — working example
use prep_pilot
db.users.insertOne({ name: "Ada", email: "ada@example.com" })
show dbsCreating a Database — mongoDB creates a database on first write after use <db> — there is no separate CREATE DATABASE DDL.
How to Explain Creating a Database to an Interviewer
How do you create a database?
use name, then insert a document