MongoDB — The Judgement Behind Document Databases
MongoDB — Embedding and Execution Plans
Goal
On a real MongoDB 8.0 running inside the Pod, you insert documents, find them, add indexes, aggregate, apply a schema, and try embedding. At the end, you read an execution plan and write it down as numbers.
Connecting
mongosh labdb
You use labdb. PostgreSQL is also running in the same Pod, so psql works as is
— if you put the two databases side by side and solve the same problem in each, the differences become
much clearer.
Result files
Some steps leave their results in files. Run mkdir -p /root/work/mongo first.
To save JSON from the shell, write it like this.
mongosh --quiet labdb --eval 'JSON.stringify(db.items.find({},{_id:0}).toArray())' > /root/work/mongo/02-query.json
Insert documents — a value can also be an array
Connect with mongosh labdb. Insert 5 or more documents with db.items.insertMany([...]). Give each document name (a string), qty (a number), and tags (an array). One of them must be named 빨간 배낭 (the Korean name means "red backpack").
items has 5 or more documents · each document has name, qty, and tags (an array)
Fetch only the fields you need
Find documents where qty is greater than 0 and project only name and qty (_id: 0). Save the result as a JSON array in /root/work/mongo/02-query.json. It is convenient to use mongosh --quiet --eval and JSON.stringify.
02-query.json contains the documents with qty>0, holding only name and qty
Create an index and see whether the plan changes
db.items.createIndex({name: 1}). Then check with db.items.find({name:'빨간 배낭'}).explain() whether IXSCAN appears in the plan.
There is an index on name, and that query uses IXSCAN
Change just one field
Change the qty of the document named 빨간 배낭 to 10. Use $set — if you assign the whole document, the fields you did not write disappear.
qty is 10, and name and tags remain unchanged
Unwind the array and group by tag
Unwind tags with $unwind and compute the qty total per tag with $group. Save the result in /root/work/mongo/05-bytag.json as [{_id, total}, ...].
The per-tag totals in 05-bytag.json match the actual values
Apply a schema in a document DB too
With db.createCollection('orders', {validator: {$jsonSchema: {...}}}), make qty required and numeric. A rule you have written down is the same as no rule if it does not actually block anything — try inserting a string yourself and see whether it is rejected.
orders has $jsonSchema validation, and a wrong type is actually rejected
Embed what is read together
Insert one order into orders, embedding the line items as a lines array (2 or more, with name and qty in each item). Include qty as well so that it passes the validation from step 6. You can search inside the array with db.orders.find({'lines.name': ...}).
One order document has 2 or more lines · it can be found by lines.name
Read the plan and write it down as numbers
In /root/work/mongo/08-explain.txt, compare and write down the case with an index (IXSCAN) and without one (COLLSCAN). Be sure to include numbers such as totalDocsExamined from explain('executionStats').
08-explain.txt has IXSCAN, COLLSCAN, and numbers written together