Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 70 ยท Getting started

The Practice Engine Used in This Tutorial

Meet the small JavaScript engine behind this tutorial's runnable examples: how it matches filters, updates documents and runs pipelines, and what it does not emulate.

MongoDB needs a server, and a web page cannot start one. So that every lesson can still run real code, this tutorial includes a practice engine: a few dozen lines of plain JavaScript that behave like a tiny MongoDB. It keeps documents in an array, decides which documents match a filter such as { year: { $gt: 1900 } }, applies update operators such as $set and $inc, and runs aggregation stages such as $match and $group.

It is a stand-in, not MongoDB. The goal is that the filter, update and pipeline documents you write in the TryIts are exactly what you would send to a real server, so what you learn transfers. This lesson opens the engine up, shows you how to read the TryIts in later lessons, and lists honestly what it leaves out.

Do not mistake the stand-in for the database. It has no indexes, no durability, no concurrency, no ObjectId generation (it uses 1, 2, 3, ...), and supports only the operators each lesson needs. Real MongoDB is far more capable and has its own edge cases. When a lesson says "real MongoDB does X", that describes MongoDB's behaviour, not the engine's.

What the engine does

Match Tests each document against a filter.
Update Applies $set, $inc and friends.
Aggregate Runs pipeline stages in order.

The inputs are the same documents you would send to MongoDB.

1 / 2

Documents are plain objects

The engine stores each document as an ordinary JavaScript object, and a collection is just an array of them. A filter is another object. The engine's job is a single function, matches(doc, filter), that answers "does this document satisfy the filter?" Three rules cover most of it:

  1. A plain value means equality: { genre: "scifi" }. If the field holds an array, the filter matches when any element equals the value, as in real MongoDB.
  2. A field name may be a dot path: { "author.name": "Austen" } reaches into nested objects.
  3. An object whose keys start with $ holds operators: { year: { $gt: 1900 } }. All conditions in a filter must hold (an implicit AND), and $and and $or combine sub-filters.

The first stand-in contains the matcher and a three-book collection. Run it, then change the filters and watch the result change.

JS The matcher: which documents match?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Two details are worth reading in the code. The comparable helper makes $gt, $lt and friends compare only values of the same type, because MongoDB does not treat null or a string as "less than" a number in range queries. And the last line shows what happens when you use an operator the engine does not know: it throws a clear error instead of quietly returning wrong results. Operators that need extra machinery, such as $regex, appear in Text Matching and Regular Expressions, where the lesson supplies its own support.

From matching to a collection

The second stand-in wraps the matcher in a Collection class with the methods you will use all tutorial long:

  • insertMany(docs) adds documents and gives each an _id (the counter 1, 2, 3, ...; real MongoDB generates an ObjectId, see _id and ObjectId).
  • find(filter, projection) returns a cursor with sort(), limit() and toArray(), chained like the real driver. The projection supports field inclusion and _id: 0.
  • updateOne(filter, update) supports $set and $inc and returns the same kind of result object as MongoDB: matchedCount and modifiedCount.
  • aggregate(pipeline) runs $match, $sort, $limit and $group with $sum and $avg.

Notice the update results. Setting pages to the value it already has matches a document but modifies nothing, so modifiedCount is 0. Real MongoDB reports the same distinction.

JS A collection with find, update and aggregate

Press Run (or Ctrl + Enter) to see the console output here.

No output.

How to read a TryIt in later lessons

Later lessons paste a compact version of the engine at the top of the code, then use it. A few habits make the examples easy to follow:

  • Read the filter, update or pipeline first. That object is the part you would also type into mongosh. The surrounding JavaScript is just plumbing to print results.
  • Compare the stand-in call with the real call. A stand-in books.find({ genre: "scifi" }).toArray() corresponds to db.books.find({ genre: "scifi" }) in the shell, which prints results automatically. Drivers use toArray() too.
  • Edit and re-run. Change a filter, add a document with a missing field, or reverse a sort. Predicting the result and then running it is the fastest way to learn.
  • Treat "not supported" errors as information. If you use an operator the pasted engine lacks, it tells you so.

When a lesson shows mongosh code in a read-only block, it is real syntax that you can paste into a real shell. When it shows a js TryIt, it is stand-in code you can run right here.

What is real and what is simplified

Topic Real MongoDB Practice engine
Storage Disk, journaled, durable Array in memory
_id ObjectId by default Counter 1, 2, 3, ...
Queries Indexes, query planner Scan every document
Operators Hundreds Only those a lesson needs
Errors Rich error codes Plain JavaScript errors
Types BSON types (Int32, Decimal128, ...) JavaScript types
Ordering of mixed types Defined BSON order Not emulated
Concurrency Many clients at once Single-threaded

That last row matters for lessons about transactions and replication. Where concurrency or distribution is the topic, the stand-ins are simulations: they model an idea such as an election or a quorum, so you can see the rules play out, but they do not reproduce the real protocol.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the practice engine's matches(doc, filter) function decide?

    matches answers one question: does this document satisfy the filter? find, update and aggregate all build on that.
  2. 2. A document has tags: ["scifi", "classic"]. Does the filter { tags: "scifi" } match it?

    Equality against an array field matches when any element equals the value. This is real MongoDB behaviour and the stand-in copies it.
  3. 3. updateOne sets a field to the value it already has. What does the result report?

    The document matched, but nothing changed, so modifiedCount stays 0.
  4. 4. Which of these does the practice engine NOT provide?

    The engine scans arrays in memory. Indexes and durability are real MongoDB features you read about, not ones the stand-in emulates.

Summary

  • The practice engine is a small JavaScript stand-in: collections are arrays, filters are objects, and matches(doc, filter) is the core.
  • Filters use equality, dot paths, $-operators, implicit AND, $and and $or; array fields match on any element.
  • Collection offers insertMany, find (with sort, limit, projection), updateOne and aggregate, with result objects shaped like MongoDB's.
  • It has no indexes, storage, concurrency or security, and uses a counter instead of ObjectId.
  • Read the filter, update or pipeline object first: that part is real MongoDB syntax.
  • Unsupported operators throw an error rather than silently misbehaving.

Now start modelling data in Documents, JSON and Field Types.