Loading

Quipoin Menu

Learn • Practice • Grow

mongodb / CLI Task Manager
interview

Q1. How would you structure a CLI task manager using MongoDB?
A task manager CLI would have commands like add, list, complete, delete. Use Mongoose for schema: task has title, description, status (pending/completed), createdAt. Connect to MongoDB at startup. Store tasks in a 'tasks' collection. Use yargs or commander for CLI parsing.

Q2. How do you add a new task from CLI?
Parse arguments and use Mongoose model:
const Task = require('./models/Task'); async function addTask(title, description) { try { const task = new Task({ title, description }); await task.save(); console.log('Task added with id:', task._id); } catch (err) { console.error('Error:', err.message); } }

Q3. How do you list tasks with filtering?
Accept flags for status (--pending, --completed). Use Mongoose queries:
const filter = {}; if (argv.pending) filter.status = 'pending'; if (argv.completed) filter.status = 'completed'; const tasks = await Task.find(filter).sort({ createdAt: -1 }); console.table(tasks.map(t => t.toObject()));

Q4. How do you mark a task as complete?
Use findByIdAndUpdate:
async function completeTask(id) { const task = await Task.findByIdAndUpdate( id, { status: 'completed' }, { new: true } ); if (task) console.log('Task completed:', task.title); else console.log('Task not found'); }

Q5. How do you handle database connection in a CLI app?
Connect once at the beginning of the script:
require('./db'); // connect mongoose // after all commands, close connection process.on('exit', () => mongoose.connection.close());
Use a separate db.js to export connection promise.