MongoDB Fundamentals
MongoDB Compass and Shell
Two ways to talk to a cluster: a GUI for exploring, a shell for the queries you will write in interviews.
Two tools, same database. Compass is the GUI: browse collections, click a document, run a query, look at an explain plan later. mongosh is the shell: the same queries you will type in an interview and paste into a backend.
Compass
- See collections and documents
- Build a filter without memorising syntax
- Inspect a single document's shape
- Useful when you do not know the data yet
mongosh (the shell)
- Run queries as code
- What interviewers expect you to write
- Same `db.users.find(...)` as the docs
- Light admin: `show dbs`, `use`, indexes later
show dbs
use app
show collections
db.users.find({ city: "Bangalore" }){ _id: ObjectId("64a1b2c3d4e5f6a7b8c90001"), name: "Rahul", city: "Bangalore", ... }use app sets the current database. Then db is app, and db.users is the collection. Compass does the same thing with a dropdown. The next two lessons are how Node.js speaks this protocol without a GUI.
Interview question
When would you use Compass versus the MongoDB shell?
Compass when I am exploring a collection I have not seen — shape, indexes, a sample document. mongosh when I need to write the actual query, debug a filter, or do something I will paste into application code.