Skip to content

CSHARP-5769: Implement hasAncestor, hasRoot, and returnScope for Atlas Search#1933

Draft
ajcvickers wants to merge 1 commit intomongodb:mainfrom
ajcvickers:CSHARP-5769
Draft

CSHARP-5769: Implement hasAncestor, hasRoot, and returnScope for Atlas Search#1933
ajcvickers wants to merge 1 commit intomongodb:mainfrom
ajcvickers:CSHARP-5769

Conversation

@ajcvickers
Copy link
Copy Markdown
Contributor

Add hasAncestor and hasRoot search operators for use within embeddedDocument queries, allowing searches to reference fields at ancestor or root document levels. Add returnScope support to $search and $searchMeta pipeline stages, enabling results to be returned from a nested embedded document scope rather than the root document. Add Clone() to SearchOptions and its nested options classes to avoid mutating caller-supplied options when returnScope is set.

@ajcvickers ajcvickers requested a review from Copilot April 2, 2026 10:26
@ajcvickers ajcvickers added the feature Adds new user-facing functionality. label Apr 2, 2026
Copy link
Copy Markdown
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 extends the driver’s Atlas Search support by adding hasAncestor/hasRoot operators (for embedded document queries) and returnScope support for $search / $searchMeta, including option cloning to prevent mutating caller-provided SearchOptions when returnScope is set.

Changes:

  • Add HasAncestor and HasRoot search operators and rendering support.
  • Add returnScope overloads for $search and $searchMeta across pipeline + fluent APIs.
  • Introduce Clone() on SearchOptions and nested option classes; add/extend Atlas Search tests and utilities.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
tests/MongoDB.Driver.Tests/Search/VectorSearchTests.cs Update test client creation to match new tuple-returning helper.
tests/MongoDB.Driver.Tests/Search/SearchDefinitionBuilderTests.cs Add rendering tests for HasAncestor / HasRoot.
tests/MongoDB.Driver.Tests/Search/AutoEmbedVectorSearchTests.cs Update test client creation to match new tuple-returning helper.
tests/MongoDB.Driver.Tests/Search/AtlasSearchTestsUtils.cs Return (IMongoClient, EventCapturer) and configure command capture for aggregates.
tests/MongoDB.Driver.Tests/Search/AtlasSearchTests.cs Add integration tests for HasAncestor/HasRoot in embeddedDocument and for returnScope in $search/$searchMeta; add test data/index setup and captured-stage validation.
src/MongoDB.Driver/Search/SearchTrackingOptions.cs Add Clone() to support safe option duplication.
src/MongoDB.Driver/Search/SearchOptions.cs Add Clone() and clone nested option objects.
src/MongoDB.Driver/Search/SearchHighlightOptions.cs Add Clone() for highlight options.
src/MongoDB.Driver/Search/SearchDefinitionBuilder.cs Add HasAncestor / HasRoot builder methods.
src/MongoDB.Driver/Search/SearchDefinition.cs Extend operator type enum with HasAncestor / HasRoot.
src/MongoDB.Driver/Search/SearchCountOptions.cs Add Clone() for count options.
src/MongoDB.Driver/Search/OperatorSearchDefinitions.cs Implement HasAncestorSearchDefinition / HasRootSearchDefinition rendering (incl. prefix reset behavior).
src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs Add $search / $searchMeta overloads with returnScope and option cloning/forced stored source.
src/MongoDB.Driver/PipelineDefinitionBuilder.cs Add pipeline extension overloads for returnScope search/searchMeta.
src/MongoDB.Driver/IAggregateFluent.cs Add fluent overloads for returnScope search/searchMeta.
src/MongoDB.Driver/AggregateFluentBase.cs Add virtual stubs + expression convenience overloads for returnScope.
src/MongoDB.Driver/AggregateFluent.cs Implement new fluent overloads delegating to pipeline extensions.
Agents.md Add repo/stack/commands/env-var guidance for agents.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…s Search

Add hasAncestor and hasRoot search operators for use within embeddedDocument
queries, allowing searches to reference fields at ancestor or root document
levels. Add returnScope support to $search and $searchMeta pipeline stages,
enabling results to be returned from a nested embedded document scope rather
than the root document. Add Clone() to SearchOptions and its nested options
classes to avoid mutating caller-supplied options when returnScope is set.
/// <returns>
/// The fluent aggregate interface.
/// </returns>
IAggregateFluent<TNewResult> Search<TNewResult>(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we easily can avoid this breaking change by implementing it as an extensions method that uses AppendStage. We can consider moving this back to the interface as a part of v4 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Adds new user-facing functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants