ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Aggregation
    • 2
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Use Case

      As a Node.js driver user building streaming pipelines (aggregation cursor → stream → batch writers) with progress logging
      I want AggregationCursor.clone() to represent an independent cursor for a variant of the same query (e.g. append $count on the clone, iterate documents on the original)
      So that I can obtain a total count and then stream the same logical result set without mutating the original cursor’s pipeline or result shape
       

      User Experience

      Expected outcome after fix or doc change:

      • Pattern cursor.clone().addStage({ $count: '…' }).next() for count, then cursor.stream() / toArray() on the original cursor, yields the same documents as if $count had never been added to the pipeline used for iteration.
      • Alternatively, if sharing the pipeline is intentional: JSDoc on clone() and addStage() clearly states that both cursors share the same pipeline array and that addStage on either cursor affects both.

      If treated as bug

      • Impacted customers: Any user of the official driver who combines clone(), addStage() (or project / match / etc.), and a second use of the original cursor.
      • Severity: Functional / data correctness — not a crash. Easy to misdiagnose as application or MongoDB query logic.
      • Blocked / broken: Not a hard blocker for the whole ecosystem; workaround is two aggregate([...]).

      Dependencies

      • None

      Risks/Unknowns

      • Behavior change: Copying the pipeline in clone() ([...this.pipeline] or shallow copy of stage objects) could break rare code that intentionally mutates cursor.pipeline on one cursor expecting the other to see it (unlikely but possible).

      Acceptance Criteria

      Implementation Requirements

      • Option A (preferred for UX): AggregationCursor.clone() passes a copy of the pipeline array to the new cursor (e.g. [...this.pipeline]), so subsequent addStage on the clone does not alter the original cursor’s pipeline (and vice versa, if addStage is allowed on uninitialized originals only).
      • Option B (minimum): No code change; document on clone() that pipeline is shared and that count + stream requires separate aggregate() calls or [...stages].

      Testing Requirements

      • Unit/integration test: fixed pipeline with $project → clone().addStage({ $count: 'count' }).toArray() → assert original toArray() / iteration still returns projected docs, count matches length (or $count value).
        I have only added a unit test on pipeline field

      Documentation Requirements

      • JSDoc on clone() (and optionally addStage()): shared or copied pipeline.

      Follow Up Requirements

      • If Node copies pipeline but other drivers share: file follow-up for cross-driver consistency or document Node-specific behavior in driver comparison docs.

            Assignee:
            Pavel Safronov
            Reporter:
            Hugues Vogel (EXT)
            None
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: