We use MongoJack to convert our POJO (plain java) classes to JSON so they can be saved easily into MongoDB. MongoJack also handles the reading of the BJSON from the MongoDB and creating the POJO again. MongoJack internally uses Jackson.
So to persist a java file we can just use the annotations
@JsonCreator
@JsonProperty
Then we save to the MongoDB with the JacksonDBCollection.
Our problem was that part of out POJO object was a byte[] that was over 16 Meg. MongoDB has a restriction on document size. So our POJO had a
byte[] _content;
Massive documents need to be persisted with GridFS in MongoDB. My approach was to leave the Java object with _content but save that part to MongoDB using GridFS. Then add contentId to our POJO to point to the ID of the GridFS saved content.
Steps
1. Stop MongoJack from persisting _content was easy. Just add this annotation to the POJO
@JsonIgnore
2. On insert save GridFS file and link to POJO with contentId
3. On load/get merely get the contentId and load the appropriated GridFS saved file and read it back into the POJO.
This method replaces the way that the POJO is saved, but leaves the POJO as is. The code does not care how the POJO is saved.
No comments:
Post a Comment