java.lang.Object
org.springframework.data.mongodb.core.mapping.ShardKey

public class ShardKey extends Object
Value object representing an entities Shard Key used to distribute documents across a sharded MongoDB cluster.
Immutable shard keys indicates a fixed value that is not updated (see MongoDB Reference: Change a Document's Shard Key Value), which allows to skip server round trips in cases where a potential shard key change might have occurred.
Since:
3.0
Author:
Christoph Strobl, Mark Paluch
  • Method Details

    • size

      public int size()
      Returns:
      the number of properties used to form the shard key.
    • getPropertyNames

      public Collection<String> getPropertyNames()
      Returns:
      the unmodifiable collection of property names forming the shard key.
    • isImmutable

      public boolean isImmutable()
      Returns:
      true if the shard key of an document does not change.
      See Also:
    • isSharded

      public boolean isSharded()
      Return whether the shard key represents a sharded key. Return false if the key is not sharded.
      Returns:
      true if the key is sharded; false otherwise.
    • getDocument

      public org.bson.Document getDocument()
      Get the raw MongoDB representation of the ShardKey.
      Returns:
      never null.
    • none

      public static ShardKey none()
      ShardKey indicating no shard key has been defined.
      Returns:
      NONE
    • range

      public static ShardKey range(String... propertyNames)
      Create a new ShardingStrategy.RANGE shard key.
      Parameters:
      propertyNames - must not be null.
      Returns:
      new instance of ShardKey.
    • hash

      public static ShardKey hash(String... propertyNames)
      Create a new ShardingStrategy.RANGE shard key.
      Parameters:
      propertyNames - must not be null.
      Returns:
      new instance of ShardKey.
    • immutable

      public static ShardKey immutable(ShardKey shardKey)
      Turn the given ShardKey into an immutable one.
      Parameters:
      shardKey - must not be null.
      Returns:
      new instance of ShardKey if the given shard key is not already immutable.