Blog
Mert Özen's blog posts on software development, backend, frontend, and DevOps.
What Is MongoDB? The Document Model, BSON, Collections and How It Differs from the Relational World
MongoDB is a document database that stores data as BSON documents with typed fields instead of table rows; data read together lives in one document, so most screens need a single read, but the model must come from the queries, not from the tables. With real output: the string date trap, BSON bytes, Decimal128, how an ObjectId is built, a concept map, WiredTiger, licensing and when not to choose it.
MongoDB Lab: a Single-Node Replica Set in Docker, mongosh, Compass and the Bookstore Data
The right MongoDB setup for development is a password-protected single-node replica set listening on 127.0.0.1; transactions and change streams only work on a replica set, and the member name must resolve from the host. With real output: the compose file and keyfile, rs.initiate, users and roles, mongosh, loading the Bookstore data with mongoimport, connecting from .NET and the transaction error on a standalone server.
CRUD in MongoDB: find, Projection, Update Operators, Upsert and Array Queries
The core of safe CRUD in MongoDB is combining the read, the check and the write in one atomic update: a conditional $inc never takes stock below zero, while read-then-write sells the same stock several times. With real output: orders matched wrongly without $elemMatch, ordered and unordered insertMany, projection, the cost of skip, update operators, positional and filtered positional updates, upsert, findOneAndUpdate, a stock race with 50 concurrent buyers and the replaceOne trap.
Document Modeling 1: Embed or Reference? From Access Patterns to Schema
Whether to embed or reference is decided not by the name of the relationship but by how often data is read together, the upper bound of an array and the need for single-document atomicity: bounded data read together is embedded, data that grows without bound or lives on its own is referenced. With real measurements: Bookstore's workload, the 16 MB limit at 110,000 reviews, the update cost of duplicated fields, the aggregate boundary and a relational copy that runs $lookup on every read.
Document Modeling 2: Schema Patterns, Schema Validation and Schema Evolution
MongoDB is not schemaless but flexible: patterns such as computed, subset, bucket, polymorphic and archive shape the model around what is read, and $jsonSchema validation protects data quality in the database; the constraint level in 9.0 guarantees that every document follows the rules. With real output: a review summary in the book, the latest 3 reviews, hourly buckets, three edition types in one collection, migration with schemaVersion, moderate versus strict and moving to constraint.
MongoDB Indexes: Compound Indexes and the ESR Rule, Multikey, Partial, TTL, Collation and explain
In MongoDB the field order of a compound index comes from the query: equality first, then sort, then range (ESR); when the range is very selective, putting it before the sort (ERS) reads fewer keys. With real explain output: COLLSCAN and IXSCAN, covered queries, $in and SORT_MERGE, multikey, unique, partial, sparse and TTL indexes, the Turkish collation trap, finding unused indexes with $indexStats and hidden indexes.
Aggregation Pipeline 1: $match, $group, $project, $unwind, $lookup and $facet
In MongoDB the aggregation pipeline passes documents through stages in order and produces new results; with $match first an index is used, while $unwind multiplies documents along an array. With real output: the $unwind mistake that doubles monthly revenue, $group results identical to the GROUP BY and HAVING results from SQL Temelleri, the optimizer reordering stages, the join strategy of $lookup and $facet for the search page.
Aggregation Pipeline 2: $setWindowFields, $graphLookup, $bucket, $merge Materialized Views and Memory Limits
In MongoDB the counterpart of window functions is $setWindowFields; but a moving average computed without adding the missing days with $densify is based on the documents that exist, not on calendar days. With real output: results identical to the running total, top-N and 7-day average from SQL Temelleri, the category tree with $graphLookup, $bucketAuto, a materialized view refreshed incrementally with $merge, the per-query memory limit in 9.0 and $function.
The MongoDB .NET Driver: MongoClient, POCO Mapping, Conventions, Guid/Decimal/DateTime and LINQ
The foundation of using the MongoDB driver correctly in .NET is keeping MongoClient as a single instance for the whole application and registering serialization rules (conventions, Guid representation) once before first use. With real output: an unconfigured Guid failing in 3.x, decimal stored as Decimal128 and DateTimeOffset as a document, the pipeline LINQ generates, a query with collation, the client-side projection error, polymorphic serialization with _t and the legacy Guid data trap.
Advanced MongoDB in .NET: Aggregation, Bulk Writes, Transactions, Change Streams and Testcontainers
In .NET, WithTransactionAsync reruns the body of a transaction on transient errors (a write conflict, for example), so the body must be short and free of side effects. With real output: the body running dozens of times for 20 concurrent orders, fluent Aggregate and BsonDocument stages, ordered and unordered BulkWriteAsync, the client-level bulk write of 8.0, a change stream in a BackgroundService that stores its resume token, and an integration test with a replica set using Testcontainers.
The MongoDB EF Core Provider: When to Use It and What It Does Not Support
The MongoDB EF Core provider is a good choice where the work is mostly CRUD and the team is used to EF Core; aggregation, change streams, bulk writes and field-level atomic updates call for the native driver. With real command output: SaveChanges rewriting owned fields every time and overwriting the driver's $inc, row versions, implicit transactions, Select and Include working in 10.0.5, and GroupBy not working.
Read Concern, Write Concern and Read Preference: Tuning Consistency in MongoDB
In MongoDB, write concern, read concern and read preference together decide how durable a write is and how fresh a read is; reading from a secondary means stale data, and read-your-writes needs majority plus a causal session. With real output from a three-member lab: a new order that is invisible on a secondary, a w:majority that timed out and was still written, 51-59 stale reads out of 200 in .NET and zero with a causal session.