Annotation Interface Sharded


The Sharded annotation provides meta information about the actual distribution of data. The shardKey() is used to distribute documents across shards.
Please see the MongoDB Documentation for more information about requirements and limitations of sharding.
Spring Data adds the shard key to filter queries used for MongoCollection.replaceOne(org.bson.conversions.Bson, Object) operations triggered by save operations on MongoOperations and ReactiveMongoOperations as well as update/upsert operations replacing/upserting a single existing document as long as the given UpdateDefinition holds a full copy of the entity.
All other operations that require the presence of the shard key in the filter query need to provide the information via the Query parameter when invoking the method.
Since:
3.0
Author:
Christoph Strobl, Mark Paluch
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    As of MongoDB 4.2 it is possible to change the shard key using update.
    The sharding strategy to use for distributing data across sharded clusters.
    The shard key determines the distribution of the collection's documents among the cluster's shards.
    Alias for shardKey().
  • Element Details

    • value

      @AliasFor("shardKey") String[] value
      Alias for shardKey().
      Returns:
      _id by default.
      See Also:
      Default:
      {}
    • shardKey

      @AliasFor("value") String[] shardKey
      The shard key determines the distribution of the collection's documents among the cluster's shards. The shard key is either a single or multiple indexed properties that exist in every document in the collection.
      By default the id property is used for sharding.
      NOTE: Required indexes are not created automatically. Create these either externally, via IndexOperations.ensureIndex(org.springframework.data.mongodb.core.index.IndexDefinition) or by annotating your domain model with Indexed/ CompoundIndex along with enabled auto index creation.
      Returns:
      an empty key by default. Which indicates to use the entities id property.
      Default:
      {}
    • shardingStrategy

      ShardingStrategy shardingStrategy
      The sharding strategy to use for distributing data across sharded clusters.
      Returns:
      ShardingStrategy.RANGE by default
      Default:
      RANGE
    • immutableKey

      boolean immutableKey
      As of MongoDB 4.2 it is possible to change the shard key using update. Using immutable shard keys avoids server round trips to obtain an entities actual shard key from the database.
      Returns:
      false by default.
      See Also:
      Default:
      false