Creating a capped collection using Spring data MongoDB @Document

2k Views Asked by At

I am trying out reactive support in spring-data with MongoDB. I am using spring-boot 2.0.0.

Generally I would write a domain object like this in my project:

@Document
public class PriceData {
    ......
}

With this spring-data it would create a collection with name priceData in MongoDB. If I want to customize it, then I would do it using the collection attribute:

@Document(collection = "MyPriceData")

Since I want to try reactive support of MongoDB, I want to create a capped collection so that I can use @Tailable cursor queries.

I can create a capped collection in my MongoDB database as specified here:

CollectionOptions options = new CollectionOptions(null, 50, true);
mongoOperations.createCollection("myCollection", options);

or

db.runCommand({ convertToCapped: 'MyPriceData', size: 9128 })

This is not a big problem if I use some external MongoDB database where I can just run this command once. But if I use an embedded MongoDB, then I would have put this in a class which would be executed every time during start up.

Either way I would be creating a collection even before the first request. So I was wondering if there is a way, I could specify to spring-data-mongodb that I need a capped collection instead of regular collection. Unfortunately @Document doesn't help in this case.

1

There are 1 best solutions below

1
Tarun Lalwani On

So below is from Oliver

Might be a good idea to have those options exposed to the @Document annotation to automatically take care of them when building the mapping context but we generally got the feedback of people wanting to manually handle those collection setup and indexing operations without too much automagic behavior. Feel free to open a JIRA in case you'd like to see that supported nevertheless.

This is back in 2011. And it seems its still true to date. If you really need the change to handle it using annotation, you should open a JIRA ticket