MongoDB CRUD Operations

Context: FIT2094_MOC · query/modify a MongoDB collection · the NoSQL counterpart of SELECT Problem it solves: create, read, update, or delete documents by a filter, using $-prefixed operators.

Quick Revision

  • 🎯 Trigger: operate on documents ➔ db.collection.command( {filter}, {action} ); an omitted or {} filter = all documents.
  • ⚡ Key Constraint: update needs $set (else it replaces the whole document); there is no ROLLBACK — a bad updateMany/deleteMany({}) is permanent.

🔧 Minimal Working Example

db.dronerent.insertOne({ "drone_id": 100, "type": { "code": "DMA2" }, "cost_per_hour": 15 });
 
db.dronerent.find(                         // READ: filter + projection
  { "drone_id": { "$eq": 103 } },
  { "type.model": 1, "pur_date": 1, "_id": 0 }  // 1=include, 0=suppress
);
 
db.dronerent.updateOne(                     // UPDATE
  { "drone_id": { "$eq": 103 } },
  { "$set": { "total_flighttime": 230 } }
);
 
db.dronerent.deleteOne({ "drone_id": { "$eq": 103 } });   // DELETE

Expected output: insert returns the _id; find returns matching docs (projection controls fields); update returns matchedCount/modifiedCount; delete returns deletedCount.

  • Collections auto-create ➔ first insert makes the collection; no CREATE TABLE, schema-less.
  • Comparison operators$eq $ne $gt $gte $lt $lte $in $nin (quoted, {field:{"$gt":0}}).
  • Logical operators$and $or $not $nor over an array of conditions.
  • Update operators$set (replace field), $inc (add to number); insertMany([...]) / updateMany / deleteMany for bulk.
  • Projection1 include / 0 suppress; _id returned unless suppressed; $slice: -1 = last array element.

🔀 Variations

  • Array element update ($ positional) ➔ update the matched RentalInfo element: filter "RentalInfo.rent_out": "…", then { "$set": { "RentalInfo.$.rent_in": "…" } }.
  • $elemMatch ➔ force multiple conditions onto the same array element: "RentalInfo": { "$elemMatch": { "rent_in": null } }.
SQLMongoDB
SELECT * FROM tdb.t.find({})
SELECT c WHERE …db.t.find({filter},{c:1})
UPDATE … SET …db.t.updateMany({filter},{$set:{…}})
DELETE … WHERE …db.t.deleteMany({filter})
DROP TABLE tdb.t.drop()

✍️ Practice

⚠️ Common Mistakes

  • 💡 No $set = full replacement{ "cost_per_hour": 55 } alone as the action replaces the entire document; always wrap in $set/$inc.
  • 💡 Empty filter hits everything, no ROLLBACKdeleteMany({}) empties the collection; there is no undo — verify the filter first.
  • 💡 modifiedCount = 0 with matchedCount > 0 ➔ the document exists but already held that value (nothing changed) — not an error.