Interface SearchCollector

All Superinterfaces:
Bson
All Known Subinterfaces:
FacetSearchCollector

@Sealed @Beta(CLIENT) public interface SearchCollector extends Bson
The core part of the $search pipeline stage of an aggregation pipeline. SearchCollectors allow returning metadata together with the search results. You may use the $$SEARCH_META variable, e.g., via Projections.computedSearchMeta(String), to extract this metadata.
Since:
4.7
MongoDB Atlas documentation
Search collectors
  • Method Details

    • facet

      @Beta({CLIENT,SERVER}) static FacetSearchCollector facet(SearchOperator operator, Iterable<? extends SearchFacet> facets)
      Returns a SearchCollector that groups results by values or ranges in the specified faceted fields and returns the count for each of those groups.
      Parameters:
      operator - The search operator to use.
      facets - The non-empty facet definitions.
      Returns:
      The requested SearchCollector.
      MongoDB Atlas documentation
      facet collector
    • of

      static SearchCollector of(Bson collector)
      Creates a SearchCollector from a Bson in situations when there is no builder method that better satisfies your needs. This method cannot be used to validate the syntax.

      Example
      The following code creates two functionally equivalent SearchCollectors, though they may not be equal.

      
        SearchCollector collector1 = SearchCollector.facet(
                SearchOperator.exists(
                        SearchPath.fieldPath("fieldName")),
                Arrays.asList(
                        SearchFacet.stringFacet(
                                "stringFacetName",
                                SearchPath.fieldPath("stringFieldName")),
                        SearchFacet.numberFacet(
                                "numberFacetName",
                                SearchPath.fieldPath("numberFieldName"),
                                Arrays.asList(10, 20, 30))));
        SearchCollector collector2 = SearchCollector.of(new Document("facet",
                new Document("operator", SearchOperator.exists(
                        SearchPath.fieldPath("fieldName")))
                        .append("facets", SearchFacet.combineToBson(Arrays.asList(
                                SearchFacet.stringFacet(
                                        "stringFacetName",
                                        SearchPath.fieldPath("stringFieldName")),
                                SearchFacet.numberFacet(
                                        "numberFacetName",
                                        SearchPath.fieldPath("numberFieldName"),
                                        Arrays.asList(10, 20, 30)))))));
       
      Parameters:
      collector - A Bson representing the required SearchCollector.
      Returns:
      The requested SearchCollector.