Running Scripts in the Shell — MongoDB Technical Interview Questions & Answers
Executing .js files against a database.
Running Scripts in the Shell
- Simple meaning: You can save commands in a .js file and run mongosh college seed.js or load("seed.js") inside the shell.
- How to use: a file with db.students.insertMany([...]) then mongosh < seed.js (or the documented --file / load).
- Good for seeding practice data.
- Common mistake: Node require() in a mongosh-only script, or running the file with node without a driver.
- Interview tip: Shell scripts for seed/admin. App code uses a driver instead.
Technical Interview Tip: A .js seed file vs an Express app.
Code Examples for Running Scripts in the Shell
Running Scripts in the Shell — working example
// Running Scripts in the Shell
db.users.find({ active: true }).sort({ name: 1 }).limit(5)Running Scripts in the Shell: find active users, sort by name, limit 5.
How to Explain Running Scripts in the Shell to an Interviewer
How do you run a shell script?
load("seed.js") or mongosh with a file