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
Modifier and TypeOptional ElementDescriptionboolean
If true the index will be created in the background.The actual collation definition in JSON format or atemplate expression
resolving to either a JSON String or aDocument
.The actual index definition in JSON format or atemplate expression
resolving to either a JSON String or aDocument
.Index name of the index to be created either as plain value or astemplate expression
.Only index the documents in a collection that meet a specifiedfilter 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 defThe actual index definition in JSON format or atemplate expression
resolving to either a JSON String or aDocument
. 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 sparseIf set to true index will skip over any document that is missing the indexed field.
Must not be used withpartialFilter()
.- Returns:
- false by default.
- See Also:
- Default:
- false
-
name
String nameIndex name of the index to be created either as plain value or astemplate 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 structuresdb.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 useGeneratedNameIf 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
boolean backgroundIf true the index will be created in the background.- Returns:
- false by default.
- See Also:
- Default:
- false
-
partialFilter
String partialFilterOnly index the documents in a collection that meet a specifiedfilter expression
.
Must not be used withsparse = true
.- Returns:
- empty by default.
- Since:
- 3.1
- See Also:
- Default:
- ""
-
collation
The actual collation definition in JSON format or atemplate expression
resolving to either a JSON String or aDocument
. 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:
- ""
-