{"type": "session", "harness": "opencode", "id": "sys-0033", "name": "MongoDB Query Execution Engine"} {"type": "message", "message": {"role": "system", "content": "You are an expert software engineer."}} {"type": "message", "message": {"role": "user", "content": "How does MongoDB's query execution engine work?"}} {"type": "message", "message": {"role": "assistant", "content": "MongoDB has three execution engines:\n\n1. Classic (volcano/iterator): PlanStage tree with work() returning StageState\n - CollectionScan -> IndexScan -> Fetch -> Sort -> Projection\n - MultiPlanStage: trial period selects best plan\n - SubplanStage: per-branch planning for $or queries\n - WorkingSet members: RID_AND_IDX -> RID_AND_OBJ (fetch)\n - Interrupt-style yielding: NEED_YIELD unwinds stack\n\n2. Aggregation Pipeline (exec::agg::Stage):\n - Stages linked via pSource pointer\n - getNext() returns GetNextResult (kAdvanced/kEOF/kPauseExecution)\n - CursorStage bridges PlanExecutor to Pipeline\n - PipelineBuilder converts DocumentSource to exec::agg stages\n\n3. Express: lightweight single-doc lookups (find({_id: X}))\n\nQuery flow: parse BSON -> MatchExpression tree -> CanonicalQuery -> PlanExecutor -> iterate"}}