KOVA
Docs

Documents

Insert, replace, update, and delete documents with the commands a MongoDB driver already sends.

A collection appears the first time you insert into it. You can also create it empty, which is how you attach an autoincrement field. Commands below assume the shell is already on the database. mongosh "mongodb://127.0.0.1:27018/shop" does that. Every command carries $db. The shell fills it in.

Insert

db.orders.insertOne({ sku: "kettle", qty: 2, tags: ["kitchen"] })
db.orders.insertMany([
  { sku: "mug", qty: 6 },
  { sku: "bowl", qty: 4 }
])

Each document is BSON. Numbers, strings, booleans, dates, nested documents, and arrays are stored as sent. Omit _id and the server assigns an ObjectId before the write is logged, so a replica set stores the same id on every member.

The wire command, if you are not using a helper:

db.runCommand({
  insert: "orders",
  documents: [{ sku: "kettle", qty: 2 }]
})

Read

db.orders.find({ sku: "kettle" })
db.orders.find({ qty: { $gt: 2 } }, { sku: 1, qty: 1, _id: 0 })
db.orders.find().sort({ qty: -1 }).skip(2).limit(5)
db.orders.countDocuments({ qty: { $gte: 2 } })

A projection includes the fields you name. _id stays unless you set it to 0. Sort, skip, and limit page through a result. The reply is a cursor: firstBatch holds the first documents, and the client asks for the rest.

explain tells you how the read ran. IDHACK is a lookup by _id alone. IXSCAN is a secondary index. COLLSCAN walks the pages.

db.orders.find({ sku: "kettle" }).explain()

Predicates, pipelines, and update operators are on Find and aggregate.

Update

A replacement overwrites the document and keeps _id.

db.orders.replaceOne({ sku: "kettle" }, { sku: "kettle", qty: 3 })

Operators change fields in place. An unknown operator is an error.

db.orders.updateOne({ sku: "kettle" }, { $set: { qty: 3, color: "black" } })
db.orders.updateOne({ sku: "kettle" }, { $inc: { qty: 1 } })
db.orders.updateOne({ sku: "kettle" }, { $unset: { color: "" } })
db.orders.updateOne({ sku: "kettle" }, { $rename: { color: "finish" } })
db.orders.updateOne({ sku: "kettle" }, { $push: { tags: "sale" } })
db.orders.updateOne({ sku: "kettle" }, { $pull: { tags: "sale" } })

updateMany applies the same change to every match. The reply includes n (matched) and nModified (changed).

Delete and drop

db.orders.deleteOne({ sku: "bowl" })
db.orders.deleteMany({ qty: { $lt: 1 } })
db.orders.drop()
db.dropDatabase()

drop removes one collection. dropDatabase removes the database you are in. Views that still read a collection block the drop of that collection until the views are dropped.

Create a collection on purpose

db.runCommand({ create: "orders" })

Pass autoincrement here when a field should fill itself. That is the whole subject of Autoincrement.

db.runCommand({ create: "orders", autoincrement: "id" })

Require a field

collMod stores a small validator on the collection. required lists field names. properties.<field>.bsonType names the BSON type. Insert and update check it.

db.runCommand({
  collMod: "orders",
  validator: {
    required: ["sku"],
    properties: { qty: { bsonType: "int" } }
  }
})

A document missing sku, or a qty that is not an int, is refused. Leave the validator off and documents stay schemaless.

Sizes

db.runCommand({ collStats: "orders" })
db.runCommand({ dbStats: 1 })
db.runCommand({ listCollections: 1 })
db.adminCommand({ listDatabases: 1 })

collStats reports the serialized size and the average document size. dbStats sums the partitions. listCollections includes views, with type: "view".