CRUD starts with insertOne/insertMany and find/findOne. Filters are documents describing matching criteria.
Insert
db.orders.insertOne({
customerId: 'c-42',
lines: [{ sku: 'A1', qty: 2 }],
status: 'open',
total: 19.98
})Practice: Run in mongosh on your practice database.
Find with equality
db.orders.find({ status: 'open' })
db.orders.findOne({ customerId: 'c-42' })
Projection preview
db.orders.find(
{ status: 'open' },
{ customerId: 1, total: 1, _id: 0 }
)
Important interview questions and answers
- Q: find vs findOne?
A: find returns a cursor (many); findOne returns first match or null. - Q: insertMany ordered?
A: ordered:false continues on duplicate key errors in bulk loads.
Self-check
- How do you return only customerId and total?
- What filter finds status open?
Tip: Projection { _id: 0 } keeps APIs tidy when ObjectId is internal.
Interview prep
- find vs findOne?
- find returns cursor; findOne returns single doc or null.
- Projection?
- Second arg selects fields returned.