db.products.insertOne({ sku: "A100", name: "Keyboard", price: 49, in_stock: true })Returns an inserted id for the created document.
Insert, find, update, replace, and delete documents with the core mongosh collection methods.
Core document insertion and lookup workflows.
db.products.insertOne({ sku: "A100", name: "Keyboard", price: 49, in_stock: true })Returns an inserted id for the created document.
db.products.insertMany([{ sku: "A101", name: "Mouse", price: 19 }, { sku: "A102", name: "Monitor", price: 199 }])Useful for seed data and batch creation workflows.
db.products.findOne({ sku: "A100" })Convenient when you expect or only need one match.
db.products.find({ in_stock: true, price: { $lt: 100 } })Filters use MongoDB query operators such as `$lt`, `$gt`, `$in`, and `$exists`.
db.products.countDocuments({ in_stock: true })Prefer `countDocuments()` for an accurate filtered count.
Common mutation patterns for documents.
Modify selected fields without replacing the whole document.
db.products.updateOne({ sku: "A100" }, { $set: { price: 59, in_stock: false } })`$set` is the most common operator for partial updates.
Apply one change to many matching documents.
db.products.updateMany({ category: "accessories" }, { $set: { featured: true } })Great for backfills and bulk flag changes.
db.products.replaceOne({ sku: "A101" }, { sku: "A101", name: "Wireless Mouse", price: 29, in_stock: true })Use when you intentionally want a full-document replacement instead of a partial mutation.
db.products.deleteOne({ sku: "A102" })Returns how many documents were deleted.
db.products.deleteMany({ discontinued: true })Useful for cleanup jobs and fixture resets.
db.products.updateOne({ sku: "A103" }, { $set: { name: "Desk Lamp", price: 39 } }, { upsert: true })Upserts are handy for idempotent seed and sync scripts.