@RequestMapping(value = "/id/", method = RequestMethod.GET)
public String getClaimsDetailById(@RequestParam(value = "userId") String userId,
@RequestParam(value = "id") String id) throws JsonProcessingException {
MongoDatabase database = this.mongoClient.getDatabase(this.database);
MongoCollection<Document> collection = database.getCollection(this.collection);
List claim = new ArrayList();
Document document = new Document("_id", new ObjectId(id));
List<Document> claims = collection.find(document).into(claim);
List<Document> claimsUpdatedList = new ArrayList<>();
for (Document doc : claims) {
if (null != doc.get("Common")) {
Document common = (Document) doc.get("Common");
if (null != common.get("EffectiveDate")) {
Date date = (Date) common.get("EffectiveDate");
common.put("EffectiveDate",convertDate(date));
}
if (null != common.get("ExpirationDate")) {
Date date = (Date) common.get("ExpirationDate");
common.put("ExpirationDate",convertDate(date));
}
doc.put("Common",common);
claimsUpdatedList.add(doc);
}
}
JsonWriterSettings writerSettings = JsonWriterSettings.builder().outputMode(JsonMode.SHELL).indent(true).build();
return claimsUpdatedList.get(0).toJson(writerSettings);
}
-
Hi, I've formatted your code. However, a code dump is not a good question. Please edit your question and describe in plain English what problem you have. What is the memory issue? Do you have a stack trace to include? See also How to Ask.Robert– Robert2020-05-26 15:01:51 +00:00Commented May 26, 2020 at 15:01
Add a comment
|
1 Answer
The problem is that you're using into(), which loads all the data at once into the List, therefore possibly overloading the heap if the data is very large.
Instead, you should be using iterator(), which uses the database cursor to process the data one-by-one, without loading them into application memory. This concept is not restricted to Mongo, it is a common mechanism with all databases (which is why the very concept of cursors exists).
For example:
...
FindIterable<Document> documentCursor = collection.find(document);
for (Document doc : documentCursor) {
...
}
3 Comments
Rose
Thank you valuable suggestion .Could you show one scenario as I am new to Mongodb
Piotr Wilkin
Added a sample modification to your code in the answer
Rose
Thank you for your response .It helped me .