I am experimenting with Java and MongoDB and I am making a lookup aggregation. I noticed that when I perform a lookup the document returned contains the lookup field as an Array of documents - as a java.util.ArrayList.
So my question here is - what if I have a case where in my lookup array I will have many documents loaded ? This may be a problem for my java heap memory ?
1 Answer
As long as the single document's size and result-set size is within permissible boundaries of your runtime, you shouldn't see any sluggishness in the response. Also keep in mind the MongoDb driver limitions of BSON Document size of 16MB and pipeline stage memory limit of 100MB without the allowDiskUse command.
Refer the MongoDb doc: https://www.mongodb.com/docs/drivers/java/sync/current/fundamentals/aggregation/
2 Comments
SpmiR
So what options do I have ? Just set allowDiskUse to true in order to be certain that I will not hit mongoDB limitations, but I will lose performance significantly ?
Subhajeet Laga
That's correct setting allowDiskUse will just push the workload onto the MongoDb's infrastructure. You can add proper indexes to your documents. As in a NoSQL DB the indexes are maintained differently as compared to SQL, as aggregation in NoSQL does use indexes and they can speed up your query execution time. Otherwise, you can definitely find a fine balance by either modularizing your document models or by keeping the aggregation results on to a new collection and regularly update this result-set. Although this is not a recommended option.
$lookupoperation always returns result as an array (as aList<T>in Java). The size of the array and the fields can be restricted, using appropriate filtering and projection - which again depends upon what data you are looking for in the result..