Skip to content

Conversation

@nirinchev
Copy link
Collaborator

Proposed changes

Fixes a few quirks with the aggregate tool:

  • If we have $merge or $out stages, don't attempt to apply limits - this will not work as we can't have stages after $merge or $out
  • Correctly check for $search and $searchMeta stages
  • Tweak the message generation to avoid repeating the same thing when the total docs equals to the number of returned docs.

@nirinchev nirinchev requested a review from a team as a code owner January 7, 2026 17:48
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issues with the aggregate tool's pipeline construction and output messaging. The changes ensure proper handling of write operations ($merge, $out) without applying limits, correct detection of search stages, and improved result messaging that avoids redundancy when document counts match.

Key changes:

  • Special-case handling for $merge and $out stages to prevent applying limits after write operations
  • Enhanced search stage detection to include $search and $searchMeta alongside $vectorSearch
  • Refined message generation to avoid redundancy when returned document count equals total document count

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/tools/mongodb/read/aggregate.ts Added early return path for write pipelines, fixed search stage detection logic, and refactored message generation to conditionally append document count details
tests/integration/tools/mongodb/read/aggregate.test.ts Updated expected messages to remove redundancy, added tests for $limit and $out stages, refactored limit tests with helper function, and removed duplicate message expectations

Comment on lines 111 to 121
if (pipeline.some((stage) => "$out" in stage || "$merge" in stage)) {
// This is a write pipeline, so special-case it and don't attempt to apply limits or caps
aggregationCursor = provider.aggregate(database, collection, pipeline);
const results = await aggregationCursor.toArray();
return {
content: formatUntrustedData(
"The aggregation pipeline executed successfully",
...(results.length > 0 ? [EJSON.stringify(results)] : [])
),
};
}
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The early return for $out/$merge pipelines bypasses the telemetry emission that occurs later in the function. This creates inconsistent telemetry reporting where write operations won't be tracked. Consider extracting the telemetry logic into a helper function and calling it before this return, or restructuring to ensure all execution paths emit telemetry.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we decouple the two different ways of doing this into different methods where you get the result and then follow same logic to return the result? this also will help us understand this business logic in the long-term

Copy link
Collaborator

@himanshusinghs himanshusinghs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

@nirinchev nirinchev enabled auto-merge (squash) January 8, 2026 11:35
@nirinchev nirinchev merged commit 7043c21 into main Jan 8, 2026
16 checks passed
@nirinchev nirinchev deleted the ni/aggregate-fixes branch January 8, 2026 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants