MongoDB Essentials
116 flashcards by MentisLibrary
Documents and BSON types, CRUD and update operators, query and aggregation pipelines, index types and the ESR rule, embedding versus referencing, replica sets, sharding and transactions.
- What is MongoDB?
- A document database that stores records as BSON documents inside collections. Documents in one collection need not share the same fields, and queries, updates and aggregations are themselves written as documents rather than as SQL text.
- BSON
- Binary JSON, the format MongoDB stores and transmits. It adds types JSON lacks, including ObjectId, Date, Decimal128, Binary and separate 32-bit and 64-bit integers, and it is length-prefixed so a scan can skip over fields it does not need.
- MongoDB collection
- A named group of documents inside a database, the rough equivalent of a table. It is created implicitly on the first insert and enforces no schema unless validation rules are attached to it.
- What does the _id field do?
- It is the primary key of a document. Every document has one, it is unique within the collection, it is immutable after insert, and MongoDB creates a unique index on it automatically.
- ObjectId
- The 12-byte default value MongoDB generates for _id when the client supplies none. The first four bytes are a Unix timestamp, so ObjectId values sort roughly in creation order.
- Map the relational terms to their MongoDB equivalents
- Database to database, table to collection, row to document, column to field, primary key to _id, and a join to either an embedded document or an aggregation $lookup.
- How large can one BSON document be?
- 16 MB. A document that would exceed the limit must be split, or the growing part moved into its own collection.
- Which BSON type should hold money?
- Decimal128, a 128-bit decimal floating point type with exact base-10 arithmetic. A Double is binary floating point and cannot represent values such as 0.1 exactly.
- Embedded document
- A document stored as the value of a field inside another document. It is read and written with its parent in a single atomic operation.
- How do you address a nested field in a query?
- With dot notation, and the path must be quoted because it is a string key.
- What does a flexible schema actually give you?
- Documents in one collection may have different fields and different shapes, so adding a field is a write rather than a migration. The server enforces nothing beyond _id unless you add validation.
- How do you enforce a schema on a collection?
- With a validator, usually written as $jsonSchema, set at collection creation or with collMod. validationLevel and validationAction decide whether it applies to updates of old documents and whether a violation errors or only warns.
- insertOne vs insertMany
- insertOne writes a single document and returns its _id. insertMany writes an array in one command; by default it is ordered and stops at the first error, and { ordered: false } makes it continue past failures.
- find vs findOne
- find returns a cursor over every matching document; findOne returns the first matching document itself, or null.
- updateOne vs updateMany
- updateOne applies the update to the first matching document; updateMany applies it to every match. Both take a filter, an update document made of operators, and options.
- What does $set do?
- Sets the value of a field, creating it if it does not exist, and leaves every other field alone. Dot notation sets nested fields and array positions.
- What does $unset do?
- Removes a field from a document. The value given in the update is ignored, by convention an empty string.
- What does $inc do?
- Adds a number to a numeric field, atomically on the server. A negative value decrements, and the field is created with the given value if it is missing.
- What does $push do?
- Appends a value to the end of an array field, creating the array if it does not exist.
- How do you push several values and cap an array's length?
- With $push plus the $each, $sort and $slice modifiers, applied in that order by the server.
- $addToSet vs $push
- $addToSet appends only if an equal value is not already present, so the array behaves like a set. $push always appends.
- $pull vs $pop
- $pull removes every array element that matches a value or a condition. $pop removes one element from an end: -1 for the first, 1 for the last.
- Upsert
- An update option that inserts a new document when the filter matches nothing. The new document is built from the filter's equality fields plus the update operators.
- What does replaceOne do?
- Replaces the entire matched document with the one given, keeping only _id. Every field not present in the replacement is gone.
- deleteOne vs deleteMany
- deleteOne removes the first matching document, deleteMany removes all of them. Both return a deletedCount.
- What does findOneAndUpdate do?
- Atomically finds one document, applies the update, and returns the document. returnDocument: 'after' returns the updated version instead of the original.
- What does bulkWrite do?
- Sends a mixed list of insert, update, replace and delete operations in one command. ordered: true stops at the first error; ordered: false attempts them all and reports the failures.
- What does the positional operator $ do in an update?
- It stands for the index of the first array element matched by the query filter, so the update can target that element.
- How do you update every array element that meets a condition?
- With the filtered positional operator $[<identifier>] and a matching arrayFilters option.
- Can an update be written as an aggregation pipeline?
- Yes. Passing an array of aggregation stages instead of an update document lets a field be computed from other fields of the same document.