Logical Structure

Storage Engine Comparison

EngineStatusUse/Notes
WiredTigerDefaultCache≈50% RAM, compression, transaction support
MMAPv1Historical/no longer usedOnly for background reference
In-MemoryEnterpriseLow latency/not persistent

WiredTiger

  • MongoDB 3.2 and later default storage engine
  • Document-level concurrency control, supports multi-document transactions
  • Supports compression algorithms (snappy and zlib)
  • Defaults to approximately 50% of physical RAM

Data Model

Embedded Model

Store associated data within the same document structure.

Advantages:

  • High query performance
  • Easy data consistency guarantee
  • Avoid complex join operations

Applicable Scenarios:

  • Data relationships are 1:1 or 1:few
  • Embedded data usually needs to be queried together with parent document

Referenced Model

Establish association relationships by storing references to other documents.

Implementation:

  • Manual reference: Store _id field of target document
  • DBRef reference: Use MongoDB’s DBRef standard format

Applicable Scenarios:

  • Data relationships are 1:many or many:many
  • Associated data needs frequent independent updates

How to Choose

Choose Embedded

  1. Data objects have containment relationship
  2. Data that needs to be read together frequently
  3. Has map-reduce, aggregation requirements

Choose Reference

  1. Embedded data would cause a lot of data duplication
  2. Need to express relatively complex many:many relationships
  3. Large hierarchical result datasets

Minimal Runnable Example

// Insert data
db.authors.insertMany([{_id:1,name:"Li Si"},{_id:2,name:"Wang Wu"}]);
db.posts.insertMany([
  {_id:101,title:"MongoDB Guide", author_id:1, tags:["db","mongo"]},
  {_id:102,title:"Modeling Practice", author_id:2, tags:["design"]}
]);

// Create index
db.posts.createIndex({author_id:1});

// $lookup query
db.posts.aggregate([
  {$match:{tags:"db"}},
  {$lookup:{from:"authors", localField:"author_id", foreignField:"_id", as:"author"}}
]);

explain() Reading Quick Reference

FieldWhat to Look ForAction
stageCOLLSCAN vs IXSCANCOLLSCAN appears → add index
nReturned / totalDocsExaminedRatioRatio≪1 → rewrite query/composite index
indexNameWhich index hitAdjust order/composite fields