Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifing a Sharded Collection with Spring Data MongoDB

I am using Spring Boot and Spring Data MongoDB to interface with an underlying sharded MongoDB cluster. My Spring Boot Application access the cluster via a mongos router.

Using Spring Data MongoDB, you can specify the collection an object is persisted to via @Document(collection = "nameOfCollection"), or it defaults to the class name (first letter lowercase). These collections do not need to exist before-hand; they can be created at runtime.

To shard a collection in MongoDB, you need to

1 - Enable sharding on the Database: sh.enableSharding("myDb")

2 - Shard the collection on a sharded database: sh.shardCollection("myDb.myCollection", {id:"hashed"})

Assuming there is an existing sharded database, does Spring Data MongoDB offer a way to shard a collection with a shard key? As far as I can tell, I cannot shard a collection with Spring, and therefore must configure the sharded collection before my Boot application runs. I find it odd that Spring would allow me to use undefined collections, but does not provide a way to configure the collection.

Edit: I have seen both Sharding with spring mongo and How configuring access to a sharded collection in spring-data for mongo? which refer more to the deployment of a sharded MongoDB cluster. This question assumes all the plumbing is there and that the collection itself simply must be sharded.

like image 299
Chris Avatar asked Oct 18 '22 02:10

Chris


2 Answers

Despite this question being old, I've got the same question, and it looks like there is away to provide custom sharding key since recently.

Annotation-based Shard Key configuration is available on spring-data-mongodb:3.x, https://docs.spring.io/spring-data/mongodb/docs/3.0.x/reference/html/#sharding

@Document("users")
@Sharded(shardKey = { "country", "userId" }) 
public class User {

    @Id
    Long id;

    @Field("userid")
    String userId;

    String country;
}

As of today spring-boot-starter-mongodb comes with 2.x version though.

like image 58
Paulius Avatar answered Oct 20 '22 23:10

Paulius


Even though this is not a Spring Data solution, a potential workaround is posed in how to execute mongo admin command from java, where DB can be acquired from a Spring MongoTemplate.

DB db = mongo.getDB("admin");
DBObject cmd = new BasicDBObject();
cmd.put("shardcollection", "testDB.x");
cmd.put("key", new BasicDBObject("userId", 1));
CommandResult result = db.command(cmd);
like image 35
Chris Avatar answered Oct 20 '22 22:10

Chris