/*
 * Copyright 2008-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb.client.model;

import com.mongodb.lang.Nullable;
import org.bson.conversions.Bson;

import java.util.concurrent.TimeUnit;

The options to apply to the creation of an index.
@mongodb.driver.manualreference/command/createIndexes Index options
Since:3.0
/** * The options to apply to the creation of an index. * * @mongodb.driver.manual reference/command/createIndexes Index options * @since 3.0 */
public class IndexOptions { private boolean background; private boolean unique; private String name; private boolean sparse; private Long expireAfterSeconds; private Integer version; private Bson weights; private String defaultLanguage; private String languageOverride; private Integer textVersion; private Integer sphereVersion; private Integer bits; private Double min; private Double max; private Double bucketSize; private Bson storageEngine; private Bson partialFilterExpression; private Collation collation; private Bson wildcardProjection;
Create the index in the background
Returns:true if should create the index in the background
/** * Create the index in the background * * @return true if should create the index in the background */
public boolean isBackground() { return background; }
Should the index should be created in the background
Params:
  • background – true if should create the index in the background
Returns:this
/** * Should the index should be created in the background * * @param background true if should create the index in the background * @return this */
public IndexOptions background(final boolean background) { this.background = background; return this; }
Gets if the index should be unique.
Returns:true if the index should be unique
/** * Gets if the index should be unique. * * @return true if the index should be unique */
public boolean isUnique() { return unique; }
Should the index should be unique.
Params:
  • unique – if the index should be unique
Returns:this
/** * Should the index should be unique. * * @param unique if the index should be unique * @return this */
public IndexOptions unique(final boolean unique) { this.unique = unique; return this; }
Gets the name of the index.
Returns:the name of the index
/** * Gets the name of the index. * * @return the name of the index */
@Nullable public String getName() { return name; }
Sets the name of the index.
Params:
  • name – of the index
Returns:this
/** * Sets the name of the index. * * @param name of the index * @return this */
public IndexOptions name(@Nullable final String name) { this.name = name; return this; }
If true, the index only references documents with the specified field
Returns:if the index should only reference documents with the specified field
/** * If true, the index only references documents with the specified field * * @return if the index should only reference documents with the specified field */
public boolean isSparse() { return sparse; }
Should the index only references documents with the specified field
Params:
  • sparse – if true, the index only references documents with the specified field
Returns:this
/** * Should the index only references documents with the specified field * * @param sparse if true, the index only references documents with the specified field * @return this */
public IndexOptions sparse(final boolean sparse) { this.sparse = sparse; return this; }
Gets the time to live for documents in the collection
Params:
  • timeUnit – the time unit
Returns:the time to live for documents in the collection
@mongodb.driver.manualtutorial/expire-data TTL
/** * Gets the time to live for documents in the collection * * @return the time to live for documents in the collection * @param timeUnit the time unit * @mongodb.driver.manual tutorial/expire-data TTL */
@Nullable public Long getExpireAfter(final TimeUnit timeUnit) { if (expireAfterSeconds == null) { return null; } return timeUnit.convert(expireAfterSeconds, TimeUnit.SECONDS); }
Sets the time to live for documents in the collection
Params:
  • expireAfter – the time to live for documents in the collection
  • timeUnit – the time unit for expireAfter
Returns:this
@mongodb.driver.manualtutorial/expire-data TTL
/** * Sets the time to live for documents in the collection * * @param expireAfter the time to live for documents in the collection * @param timeUnit the time unit for expireAfter * @return this * @mongodb.driver.manual tutorial/expire-data TTL */
public IndexOptions expireAfter(@Nullable final Long expireAfter, final TimeUnit timeUnit) { if (expireAfter == null) { this.expireAfterSeconds = null; } else { this.expireAfterSeconds = TimeUnit.SECONDS.convert(expireAfter, timeUnit); } return this; }
Gets the index version number.
Returns:the index version number
/** * Gets the index version number. * * @return the index version number */
@Nullable public Integer getVersion() { return this.version; }
Sets the index version number.
Params:
  • version – the index version number
Returns:this
/** * Sets the index version number. * * @param version the index version number * @return this */
public IndexOptions version(@Nullable final Integer version) { this.version = version; return this; }
Gets the weighting object for use with a text index

A document that represents field and weight pairs. The weight is an integer ranging from 1 to 99,999 and denotes the significance of the field relative to the other indexed fields in terms of the score.

Returns:the weighting object
@mongodb.driver.manualtutorial/control-results-of-text-search Control Search Results with Weights
/** * Gets the weighting object for use with a text index * * <p>A document that represents field and weight pairs. The weight is an integer ranging from 1 to 99,999 and denotes the significance * of the field relative to the other indexed fields in terms of the score.</p> * * @return the weighting object * @mongodb.driver.manual tutorial/control-results-of-text-search Control Search Results with Weights */
@Nullable public Bson getWeights() { return weights; }
Sets the weighting object for use with a text index.

An document that represents field and weight pairs. The weight is an integer ranging from 1 to 99,999 and denotes the significance of the field relative to the other indexed fields in terms of the score.

Params:
  • weights – the weighting object
Returns:this
@mongodb.driver.manualtutorial/control-results-of-text-search Control Search Results with Weights
/** * Sets the weighting object for use with a text index. * * <p>An document that represents field and weight pairs. The weight is an integer ranging from 1 to 99,999 and denotes the significance * of the field relative to the other indexed fields in terms of the score.</p> * * @param weights the weighting object * @return this * @mongodb.driver.manual tutorial/control-results-of-text-search Control Search Results with Weights */
public IndexOptions weights(@Nullable final Bson weights) { this.weights = weights; return this; }
Gets the language for a text index.

The language that determines the list of stop words and the rules for the stemmer and tokenizer.

Returns:the language for a text index.
@mongodb.driver.manualreference/text-search-languages Text Search languages
/** * Gets the language for a text index. * * <p>The language that determines the list of stop words and the rules for the stemmer and tokenizer.</p> * * @return the language for a text index. * @mongodb.driver.manual reference/text-search-languages Text Search languages */
@Nullable public String getDefaultLanguage() { return defaultLanguage; }
Sets the language for the text index.

The language that determines the list of stop words and the rules for the stemmer and tokenizer.

Params:
  • defaultLanguage – the language for the text index.
Returns:this
@mongodb.driver.manualreference/text-search-languages Text Search languages
/** * Sets the language for the text index. * * <p>The language that determines the list of stop words and the rules for the stemmer and tokenizer.</p> * * @param defaultLanguage the language for the text index. * @return this * @mongodb.driver.manual reference/text-search-languages Text Search languages */
public IndexOptions defaultLanguage(@Nullable final String defaultLanguage) { this.defaultLanguage = defaultLanguage; return this; }
Gets the name of the field that contains the language string.

For text indexes, the name of the field, in the collection's documents, that contains the override language for the document.

Returns:the name of the field that contains the language string.
@mongodb.driver.manualtutorial/specify-language-for-text-index/#specify-language-field-text-index-example Language override
/** * Gets the name of the field that contains the language string. * * <p>For text indexes, the name of the field, in the collection's documents, that contains the override language for the document.</p> * * @return the name of the field that contains the language string. * @mongodb.driver.manual tutorial/specify-language-for-text-index/#specify-language-field-text-index-example Language override */
@Nullable public String getLanguageOverride() { return languageOverride; }
Sets the name of the field that contains the language string.

For text indexes, the name of the field, in the collection's documents, that contains the override language for the document.

Params:
  • languageOverride – the name of the field that contains the language string.
Returns:this
@mongodb.driver.manualtutorial/specify-language-for-text-index/#specify-language-field-text-index-example Language override
/** * Sets the name of the field that contains the language string. * * <p>For text indexes, the name of the field, in the collection's documents, that contains the override language for the document.</p> * * @param languageOverride the name of the field that contains the language string. * @return this * @mongodb.driver.manual tutorial/specify-language-for-text-index/#specify-language-field-text-index-example Language override */
public IndexOptions languageOverride(@Nullable final String languageOverride) { this.languageOverride = languageOverride; return this; }
The text index version number.
Returns:the text index version number.
/** * The text index version number. * * @return the text index version number. */
@Nullable public Integer getTextVersion() { return textVersion; }
Set the text index version number.
Params:
  • textVersion – the text index version number.
Returns:this
/** * Set the text index version number. * * @param textVersion the text index version number. * @return this */
public IndexOptions textVersion(@Nullable final Integer textVersion) { this.textVersion = textVersion; return this; }
Gets the 2dsphere index version number.
Returns:the 2dsphere index version number
/** * Gets the 2dsphere index version number. * * @return the 2dsphere index version number */
@Nullable public Integer getSphereVersion() { return sphereVersion; }
Sets the 2dsphere index version number.
Params:
  • sphereVersion – the 2dsphere index version number.
Returns:this
/** * Sets the 2dsphere index version number. * * @param sphereVersion the 2dsphere index version number. * @return this */
public IndexOptions sphereVersion(@Nullable final Integer sphereVersion) { this.sphereVersion = sphereVersion; return this; }
Gets the number of precision of the stored geohash value of the location data in 2d indexes.
Returns:the number of precision of the stored geohash value
/** * Gets the number of precision of the stored geohash value of the location data in 2d indexes. * * @return the number of precision of the stored geohash value */
@Nullable public Integer getBits() { return bits; }
Sets the number of precision of the stored geohash value of the location data in 2d indexes.
Params:
  • bits – the number of precision of the stored geohash value
Returns:this
/** * Sets the number of precision of the stored geohash value of the location data in 2d indexes. * * @param bits the number of precision of the stored geohash value * @return this */
public IndexOptions bits(@Nullable final Integer bits) { this.bits = bits; return this; }
Gets the lower inclusive boundary for the longitude and latitude values for 2d indexes..
Returns:the lower inclusive boundary for the longitude and latitude values.
/** * Gets the lower inclusive boundary for the longitude and latitude values for 2d indexes.. * * @return the lower inclusive boundary for the longitude and latitude values. */
@Nullable public Double getMin() { return min; }
Sets the lower inclusive boundary for the longitude and latitude values for 2d indexes..
Params:
  • min – the lower inclusive boundary for the longitude and latitude values
Returns:this
/** * Sets the lower inclusive boundary for the longitude and latitude values for 2d indexes.. * * @param min the lower inclusive boundary for the longitude and latitude values * @return this */
public IndexOptions min(@Nullable final Double min) { this.min = min; return this; }
Gets the upper inclusive boundary for the longitude and latitude values for 2d indexes..
Returns:the upper inclusive boundary for the longitude and latitude values.
/** * Gets the upper inclusive boundary for the longitude and latitude values for 2d indexes.. * * @return the upper inclusive boundary for the longitude and latitude values. */
@Nullable public Double getMax() { return max; }
Sets the upper inclusive boundary for the longitude and latitude values for 2d indexes..
Params:
  • max – the upper inclusive boundary for the longitude and latitude values
Returns:this
/** * Sets the upper inclusive boundary for the longitude and latitude values for 2d indexes.. * * @param max the upper inclusive boundary for the longitude and latitude values * @return this */
public IndexOptions max(@Nullable final Double max) { this.max = max; return this; }
Gets the specified the number of units within which to group the location values for geoHaystack Indexes
Returns:the specified the number of units within which to group the location values for geoHaystack Indexes
@mongodb.driver.manualcore/geohaystack/ geoHaystack Indexes
/** * Gets the specified the number of units within which to group the location values for geoHaystack Indexes * * @return the specified the number of units within which to group the location values for geoHaystack Indexes * @mongodb.driver.manual core/geohaystack/ geoHaystack Indexes */
@Nullable public Double getBucketSize() { return bucketSize; }
Sets the specified the number of units within which to group the location values for geoHaystack Indexes
Params:
  • bucketSize – the specified the number of units within which to group the location values for geoHaystack Indexes
Returns:this
@mongodb.driver.manualcore/geohaystack/ geoHaystack Indexes
/** * Sets the specified the number of units within which to group the location values for geoHaystack Indexes * * @param bucketSize the specified the number of units within which to group the location values for geoHaystack Indexes * @return this * @mongodb.driver.manual core/geohaystack/ geoHaystack Indexes */
public IndexOptions bucketSize(@Nullable final Double bucketSize) { this.bucketSize = bucketSize; return this; }
Gets the storage engine options document for this index.
Returns:the storage engine options
@mongodb.server.release3.0
/** * Gets the storage engine options document for this index. * * @return the storage engine options * @mongodb.server.release 3.0 */
@Nullable public Bson getStorageEngine() { return storageEngine; }
Sets the storage engine options document for this index.
Params:
  • storageEngine – the storage engine options
Returns:this
@mongodb.server.release3.0
/** * Sets the storage engine options document for this index. * * @param storageEngine the storage engine options * @return this * @mongodb.server.release 3.0 */
public IndexOptions storageEngine(@Nullable final Bson storageEngine) { this.storageEngine = storageEngine; return this; }
Get the filter expression for the documents to be included in the index or null if not set
Returns:the filter expression for the documents to be included in the index or null if not set
@mongodb.server.release3.2
Since:3.2
/** * Get the filter expression for the documents to be included in the index or null if not set * * @return the filter expression for the documents to be included in the index or null if not set * @mongodb.server.release 3.2 * @since 3.2 */
@Nullable public Bson getPartialFilterExpression() { return partialFilterExpression; }
Sets the filter expression for the documents to be included in the index
Params:
  • partialFilterExpression – the filter expression for the documents to be included in the index
Returns:this
@mongodb.server.release3.2
Since:3.2
/** * Sets the filter expression for the documents to be included in the index * * @param partialFilterExpression the filter expression for the documents to be included in the index * @return this * @mongodb.server.release 3.2 * @since 3.2 */
public IndexOptions partialFilterExpression(@Nullable final Bson partialFilterExpression) { this.partialFilterExpression = partialFilterExpression; return this; }
Returns the collation options
Returns:the collation options
Since:3.4
@mongodb.server.release3.4
/** * Returns the collation options * * @return the collation options * @since 3.4 * @mongodb.server.release 3.4 */
@Nullable public Collation getCollation() { return collation; }
Sets the collation options

A null value represents the server default.

Params:
  • collation – the collation options to use
Returns:this
Since:3.4
@mongodb.server.release3.4
/** * Sets the collation options * * <p>A null value represents the server default.</p> * @param collation the collation options to use * @return this * @since 3.4 * @mongodb.server.release 3.4 */
public IndexOptions collation(@Nullable final Collation collation) { this.collation = collation; return this; }
Gets the wildcard projection of a wildcard index
Returns:the wildcard projection
@mongodb.server.release4.2
Since:3.10
/** * Gets the wildcard projection of a wildcard index * * @return the wildcard projection * @mongodb.server.release 4.2 * @since 3.10 */
public Bson getWildcardProjection() { return wildcardProjection; }
Sets the wildcard projection of a wildcard index
Params:
  • wildcardProjection – the wildcard projection
Returns:this
@mongodb.server.release4.2
Since:3.10
/** * Sets the wildcard projection of a wildcard index * * @param wildcardProjection the wildcard projection * @return this * @mongodb.server.release 4.2 * @since 3.10 */
public IndexOptions wildcardProjection(final Bson wildcardProjection) { this.wildcardProjection = wildcardProjection; return this; } @Override public String toString() { return "IndexOptions{" + "background=" + background + ", unique=" + unique + ", name='" + name + '\'' + ", sparse=" + sparse + ", expireAfterSeconds=" + expireAfterSeconds + ", version=" + version + ", weights=" + weights + ", defaultLanguage='" + defaultLanguage + '\'' + ", languageOverride='" + languageOverride + '\'' + ", textVersion=" + textVersion + ", sphereVersion=" + sphereVersion + ", bits=" + bits + ", min=" + min + ", max=" + max + ", bucketSize=" + bucketSize + ", storageEngine=" + storageEngine + ", partialFilterExpression=" + partialFilterExpression + ", collation=" + collation + ", wildcardProjection=" + wildcardProjection + '}'; } }