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.
To create a capped collection, we use the normal createCollection command but with capped option as true and specifying the maximum size of collection in bytes. This code would convert our existing collection posts to a capped collection.
capped: It is used to create a capped collection. If the value of this field is true then you must specify the maximum size in the size field. It is of boolean type.
No, You can convert a non-capped collection to a capped collection using the "convertToCapped" command but there's no way to go the other way. Your only option is to clone the collection to a non capped one and rename it which obviously involves downtime.
You can check whether a collection is capped using the isCapped() method. This method returns true as the output if the specified collection is a capped one.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With