4

I'm working on a simple project. I'm using SpringData and MongoDB.

Everything is perfect creating normal collections, but now I have to register information, I mean a logging functionality.

So I read this in the mongo documentation:

Capped collections provide a high-performance means for storing logging documents in the database. Inserting objects in an unindexed capped collection will be close to the speed of logging to a filesystem. Additionally, with the built-in FIFO mechanism, you are not at risk of using excessive disk space for the logging.

I thought great! this is what I need, but I have a doubt. Is posible to create this kind of collections with SpringData??? I couldn't find anything in SpringData documentation.

Someone knows something about this?

Thanks

hsz
  • 148,279
  • 62
  • 259
  • 315
KCOtzen
  • 866
  • 1
  • 9
  • 11

3 Answers3

7

There's a method createCollection(…) taking a CollectionOptions argument where you can specify a collection to be capped:

// The 'true' is setting it to capped
CollectionOptions options = new CollectionOptions(null, 50, true);
mongoOperations.createCollection("myCollection", options);

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.

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
2

CollectionOptions options = new CollectionOptions(null, 5000, true) is now deprecated. Instead you should use the following code:

CollectionOptions options = CollectionOptions.empty()
                    .capped().size(5242880)
                    .maxDocuments(5000)

Don't forget to specify the size. For more info see Tailable Cursors.

Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
Tombery
  • 266
  • 2
  • 15
0

If you have collection created by spring-data (for example: reservation), you can easily convert it to capped, just like so:

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

read mongodb manual: https://docs.mongodb.com/manual/reference/command/convertToCapped/

ps: @Tailable annotation is very sexy, it can help you track updates for that collection and react on it changes using reactive programming principles

Maksim Kostromin
  • 3,273
  • 1
  • 32
  • 30