Xegara
Xegara

Reputation: 131

How to insert an element to a collection field in MongoDB using Spring Data scalably?

Suppose we have a MongoDB collection called Threads where it has a field of typed collection for replies to the original post.

When the user hits the reply, we would want to create a new post instance and append it to the replies field. It can be easily done as follows:

var thread = threadRepository.findById(threadId);
thread.getReplies().add(post);
threadRepository.save(thread);

But a question arises, is this solution scalable? What if there are 1 million replies to that thread?

My main question is: Will they all be loaded in memory?

If yes, wouldn't it be a waste if all we wanted to do is create a new reply? What is the recommended solution?

Upvotes: 0

Views: 1015

Answers (1)

Christoph Strobl
Christoph Strobl

Reputation: 6736

If all the replies are nested within the thread document, then yes, they will be loaded into memory unless you explicitly specify which fields to load via @Query(fields=...)

In order to modify the Document without having to load it into memory consider an update instead of a replace operation.

Update update = new Update().push("replies", post);
template.updateFirst(query(where("id").is(thread.id)), update, Thread.class)

With $push it's possible to append an item to an array. Please see the MongoDB reference documentation for more details.

In case the intention is to potentially store millions of replies that way, then please keep at least the Document max size limit in mind and consider a feasible loading strategy.

Upvotes: 2

Related Questions