Interface SearchCollector

All Superinterfaces:
org.bson.conversions.Bson
All Known Subinterfaces:
FacetSearchCollector

@Sealed @Beta(CLIENT) public interface SearchCollector extends org.bson.conversions.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
  • Field Summary

    Fields inherited from interface org.bson.conversions.Bson

    DEFAULT_CODEC_REGISTRY
  • Method Summary

    Static Methods
    Modifier and Type
    Method
    Description
    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.
    of(org.bson.conversions.Bson collector)
    Creates a SearchCollector from a Bson in situations when there is no builder method that better satisfies your needs.

    Methods inherited from interface org.bson.conversions.Bson

    toBsonDocument, toBsonDocument
  • 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(org.bson.conversions.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.