MongoDB CRUD for Documents

Insert, find, update, replace, and delete documents with the core mongosh collection methods.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Create and read

Core document insertion and lookup workflows.

Insert one document

Create a single document.

javascriptANYmongodbinsertOnecrud
javascript
db.products.insertOne({ sku: "A100", name: "Keyboard", price: 49, in_stock: true })
Notes

Returns an inserted id for the created document.

Insert many documents

Create multiple documents in one call.

javascriptANYmongodbinsertManycrud
javascript
db.products.insertMany([{ sku: "A101", name: "Mouse", price: 19 }, { sku: "A102", name: "Monitor", price: 199 }])
Notes

Useful for seed data and batch creation workflows.

Find one matching document

Return a single document by filter.

javascriptANYmongodbfindOnecrud
javascript
db.products.findOne({ sku: "A100" })
Notes

Convenient when you expect or only need one match.

Find documents with a filter

Return all matching documents.

javascriptANYmongodbfindfilter
javascript
db.products.find({ in_stock: true, price: { $lt: 100 } })
Notes

Filters use MongoDB query operators such as `$lt`, `$gt`, `$in`, and `$exists`.

Count matching documents

Count documents for a specific filter.

javascriptANYmongodbcountDocuments
javascript
db.products.countDocuments({ in_stock: true })
Notes

Prefer `countDocuments()` for an accurate filtered count.

Update and delete

Common mutation patterns for documents.

Update one document with $set

Modify selected fields without replacing the whole document.

javascriptANYmongodbupdateOneset
javascript
db.products.updateOne({ sku: "A100" }, { $set: { price: 59, in_stock: false } })
Notes

`$set` is the most common operator for partial updates.

Update many documents

Apply one change to many matching documents.

javascriptANYmongodbupdateManybulk
javascript
db.products.updateMany({ category: "accessories" }, { $set: { featured: true } })
Notes

Great for backfills and bulk flag changes.

Replace one document

Swap the entire document body.

javascriptANYmongodbreplaceOnecrud
javascript
db.products.replaceOne({ sku: "A101" }, { sku: "A101", name: "Wireless Mouse", price: 29, in_stock: true })
Notes

Use when you intentionally want a full-document replacement instead of a partial mutation.

Delete one document

Remove the first matching document.

javascriptANYmongodbdeleteOnecrud
javascript
db.products.deleteOne({ sku: "A102" })
Notes

Returns how many documents were deleted.

Delete many documents

Remove all matching documents.

javascriptANYmongodbdeleteManycleanup
javascript
db.products.deleteMany({ discontinued: true })
Notes

Useful for cleanup jobs and fixture resets.

Upsert a document

Insert the document if no match exists.

javascriptANYmongodbupsertupdateOne
javascript
db.products.updateOne({ sku: "A103" }, { $set: { name: "Desk Lamp", price: 39 } }, { upsert: true })
Notes

Upserts are handy for idempotent seed and sync scripts.

Recommended next

No recommendations yet.