Annotation Interface CompoundIndex


@Target(TYPE) @Documented @Repeatable(CompoundIndexes.class) @Retention(RUNTIME) public @interface CompoundIndex
Mark a class to use compound indexes.

NOTE: This annotation is repeatable according to Java 8 conventions using CompoundIndexes.value() as container.

@Document
@CompoundIndex(def = "{'firstname': 1, 'lastname': 1}")
@CompoundIndex(def = "{'address.city': 1, 'address.street': 1}")
class Person {
        String firstname;
        String lastname;

        Address address;
}
Author:
Jon Brisbin, Oliver Gierke, Philipp Schneider, Johno Crawford, Christoph Strobl, Dave Perryman, Stefan Tirea
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Deprecated, for removal: This API element is subject to removal in a future version.
    since 5.0 for removal without replacement.
    The actual collation definition in JSON format or a template expression resolving to either a JSON String or a Document.
    The actual index definition in JSON format or a template expression resolving to either a JSON String or a Document.
    Index name of the index to be created either as plain value or as template expression.
    Only index the documents in a collection that meet a specified filter expression.
    boolean
    If set to true index will skip over any document that is missing the indexed field.
    boolean
     
    boolean
    If set to true then MongoDB will ignore the given index name and instead generate a new name.
  • Element Details

    • def

      String def
      The actual index definition in JSON format or a template expression resolving to either a JSON String or a Document. The keys of the JSON document are the fields to be indexed, the values define the index direction (1 for ascending, -1 for descending).
      If left empty on nested document, the whole document will be indexed.
      @Document
      @CompoundIndex(def = "{'h1': 1, 'h2': 1}")
      class JsonStringIndexDefinition {
        String h1, h2;
      }
      
      @Document
      @CompoundIndex(def = "#{T(org.bson.Document).parse("{ 'h1': 1, 'h2': 1 }")}")
      class ExpressionIndexDefinition {
        String h1, h2;
      }
      
      Returns:
      empty String by default.
      Default:
      ""
    • unique

      boolean unique
      Returns:
      false by default.
      See Also:
      Default:
      false
    • sparse

      boolean sparse
      If set to true index will skip over any document that is missing the indexed field.
      Must not be used with partialFilter().
      Returns:
      false by default.
      See Also:
      Default:
      false
    • name

      String name
      Index name of the index to be created either as plain value or as template expression.

      The name will only be applied as is when defined on root level. For usage on nested or embedded structures the provided name will be prefixed with the path leading to the entity.

      The structure below
      @Document
      class Root {
              Hybrid hybrid;
              Nested nested;
      }
      
      @Document
      @CompoundIndex(name = "compound_index", def = "{'h1': 1, 'h2': 1}")
      class Hybrid {
              String h1, h2;
      }
      
      @CompoundIndex(name = "compound_index", def = "{'n1': 1, 'n2': 1}")
      class Nested {
              String n1, n2;
      }
      
      resolves in the following index structures
      db.root.createIndex( { hybrid.h1: 1, hybrid.h2: 1 } , { name: "hybrid.compound_index" } )
      db.root.createIndex( { nested.n1: 1, nested.n2: 1 } , { name: "nested.compound_index" } )
      db.hybrid.createIndex( { h1: 1, h2: 1 } , { name: "compound_index" } )
      
      Returns:
      empty String by default.
      Default:
      ""
    • useGeneratedName

      boolean useGeneratedName
      If set to true then MongoDB will ignore the given index name and instead generate a new name. Defaults to false.
      Returns:
      false by default
      Since:
      1.5
      Default:
      false
    • background

      @Deprecated(since="5.0", forRemoval=true) boolean background
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 5.0 for removal without replacement.
      If true the index will be created in the background.

      NOTE: Since MongoDB 4.2 the background flag is ignored by the server if set.

      Returns:
      false by default.
      See Also:
      Default:
      false
    • partialFilter

      String partialFilter
      Only index the documents in a collection that meet a specified filter expression.
      Must not be used with sparse = true.
      Returns:
      empty by default.
      Since:
      3.1
      See Also:
      Default:
      ""
    • collation

      @AliasFor(annotation=Collation.class, attribute="value") String collation
      The actual collation definition in JSON format or a template expression resolving to either a JSON String or a Document. The keys of the JSON document are configuration options for the collation (language-specific rules for string comparison) to be applied on string properties being part of the index.

      NOTE: Overrides Document.collation().

      Returns:
      empty String by default.
      Since:
      4.0
      See Also:
      Default:
      ""