I'm developing a log register using mongoDB and Java SpringData.
Here MongoDb capped sub-collection talks about mongoDB structure, but I would do with Java. The most important thing it's that I have a document with one or more fields and a capped array.
Is there some method or way in Java to do this?
My object it's like:
user = {
name: String,
latest_messages: [String] (capped to a 100 elements)
}
in Java:
public class MessageLog {
private ObjectId id;
private String name;
private List<Message> messages;
}
Where:
public class Message{
private String text;
private String level;
private Date date;
}
EDIT:
I'm using Java with Spring source (mongodb driver v2.10.1: http://central.maven.org/maven2/org/mongodb/mongo-java-driver/2.10.1/)
As you have noticed, MongoDB can't provide this functionality, so it has to be solved on the application layer.
Java by default doesn't support anything which works like a capped collection. But the collection library of the Apache Commons project has the class CircularFiFoBuffer which seems to be what you need. You create it with a max size. When you add a new element and it already is at the max size, the oldest element is discarded. Iteration order is from the oldest element to the newest.
A workaround without external libraries could be done with a LinkedList. Add new elements using the addFirst() method, check the size, and when it's larger than the desired maximum size, call removeLast().
I finally get the solution using this code:
I hope it will be useful to someone else!!!