Annotation Interface Indexed
Mark a field to be indexed using MongoDB's indexing feature.
- Author:
- Jon Brisbin, Oliver Gierke, Philipp Schneider, Johno Crawford, Thomas Darimont, Christoph Strobl, Jordi Llach, Mark Paluch, 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 index sort direction.Alternative forexpireAfterSeconds()
to configure the timeout after which the document should expire.int
Configures the number of seconds after which the collection should expire.Index name 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
If set to true reject all documents that contain a duplicate value for the indexed field.boolean
If set to true then MongoDB will ignore the given index name and instead generate a new name.
-
Element Details
-
unique
boolean uniqueIf set to true reject all documents that contain a duplicate value for the indexed field.- Returns:
- false by default.
- See Also:
- Default:
- false
-
direction
IndexDirection directionThe index sort direction.- Returns:
IndexDirection.ASCENDING
by default.
- Default:
- ASCENDING
-
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 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 class Hybrid { @Indexed(name="index") String h1; @Indexed(name="#{@myBean.indexName}") String h2; } class Nested { @Indexed(name="index") String n1; }
db.root.createIndex( { hybrid.h1: 1 } , { name: "hybrid.index" } ) db.root.createIndex( { nested.n1: 1 } , { name: "nested.index" } ) db.hybrid.createIndex( { h1: 1} , { name: "index" } ) db.hybrid.createIndex( { h2: 1} , { name: the value myBean.getIndexName() returned } )
- 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
-
expireAfterSeconds
int expireAfterSecondsConfigures the number of seconds after which the collection should expire. Defaults to -1 for no expiry.- Returns:
- -1 by default.
- See Also:
- Default:
- -1
-
expireAfter
String expireAfterAlternative forexpireAfterSeconds()
to configure the timeout after which the document should expire. Defaults to an emptyString
for no expiry. Accepts numeric values followed by their unit of measure:- d: Days
- h: Hours
- m: Minutes
- s: Seconds
- Alternatively: A Spring template expression. The expression can result in a
Duration
or a valid expirationString
according to the already mentioned conventions.
@Indexed(expireAfter = "10s") String expireAfterTenSeconds; @Indexed(expireAfter = "1d") String expireAfterOneDay; @Indexed(expireAfter = "P2D") String expireAfterTwoDays; @Indexed(expireAfter = "#{@mySpringBean.timeout}") String expireAfterTimeoutObtainedFromSpringBean;
- Returns:
- empty by default.
- Since:
- 2.2
- Default:
- ""
-
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) applied to the indexed based on the field value.NOTE: Overrides
Document.collation()
.- Returns:
- empty by default.
- Since:
- 4.0
- See Also:
- Default:
- ""
-