Domain 3: CRUD Operations¶
Weighted 3%, the smallest domain. One objective: create, read, update and delete documents with the right methods and operators, and query with equality, comparison, logical and array constraints. Commands were run on MongoDB 8.3.13 on 2026-11-07.
Table of Contents¶
- The four verbs
- Updating with operators
- Querying arrays
- Bulk inserts and errors
- How the exam tests this domain
- Key terms
The four verbs¶
| Verb | Methods |
|---|---|
| Create | insertOne(), insertMany() |
| Read | find() (plus findOne()) |
| Update | updateOne(), updateMany(), replaceOne() |
| Delete | deleteOne(), deleteMany() |
Insert creates the collection if it does not exist. Every write targets a single collection, and "all write operations in MongoDB are atomic on the level of a single document". That sentence is the reason embedding gives you atomic updates to related data, a point that returns in domain 4.
π MongoDB CRUD Operations - the methods per verb and the single-document atomicity rule
Updating with operators¶
An update document uses operators such as $set and $push to change fields without replacing the whole document. replaceOne() swaps the entire document, so fields you leave out vanish. Use updateOne() for one match and updateMany() for all matches.
db.orders.updateOne({_id: 3}, {$set: {status: "paid"}, $push: {items: {sku: "b", qty: 3, price: 20}}}).modifiedCount
Output:
1
One call changed a scalar and appended to an array in a single atomic document write.
π Update Operators - $set, $push and the rest of the update operator families
Querying arrays¶
Filters use comparison operators ($gt, $gte, $in), logical operators ($and, $or, $not) and equality. Array fields add one subtlety. A plain condition on an array's fields can be satisfied by different elements. $elemMatch requires one element to satisfy all its conditions.
db.orders.find({"items.sku": "b", status: {$in: ["paid", "new"]}}, {_id: 1})
db.orders.find({items: {$elemMatch: {sku: "a", qty: {$gte: 2}}}}, {_id: 1})
Output:
[ { _id: 1 }, { _id: 3 } ]
[ { _id: 1 } ]
The first query matches orders that contain sku b and have status paid or new (orders 1 and 3). The second needs a single line with sku a and quantity of at least 2 (order 1 only).
π Query an Array - exact match, element conditions and $elemMatch
Bulk inserts and errors¶
insertMany() is ordered by default: it stops at the first error. With {ordered: false} it keeps going and reports the failures at the end. Inserting _id: 1 (a duplicate) and _id: 9 unordered gave:
MongoBulkWriteError: E11000 duplicate key error collection: shop.orders index: _id_ dup key: { _id: 1 }
and a later read showed [ 1, 3, 9 ], so document 9 was still inserted. This ordered/unordered behavior is general MongoDB knowledge; the CRUD overview page above lists bulk writes but this detail was shown by the run, not quoted from a page.
How the exam tests this domain¶
- Pick the method: one match or many, insert or replace.
- Pick the operator:
$setchanges a field,$pushappends to an array. - Array filters: know that
$elemMatchties multiple conditions to one element.
Key terms¶
- CRUD - create, read, update and delete, the four basic data operations.
- Update operator - a
$-prefixed instruction in an update document, such as$setor$push. replaceOne()- a method that replaces an entire matching document.$elemMatch- a query operator requiring at least one array element to satisfy all the listed conditions.- Atomic - all-or-nothing; MongoDB writes are atomic at the level of a single document.