1

I've a project based on a mongodb database and I need to accomplish the following description in a very optimized way. My database has a Document with another embedded documents and I need to apply pagination in this embedded documents, eg:

{ _id: "str", .... , emebedded_doc_example = [ {},{},{} ] }

The problem is that at first we didn't expect that embedded_doc_example will grow as much as it is. So I want to apply pagination and filtering on server side on the embedded_doc_example. Then I'm seeing two solutions:

1.Get all the embedded documents of a give parent's _id and then apply the filters and the pagination in the list.

2.Separate the emebedded_docs_example in another doc and create a relation to the old parent.

The problem is better for me since it doesn't requires a lot of refactoring. However, I don't know if this will improve the results a lot since I'm loading all the embedded objects of a given parent in memory. Am I right?

Miguel Andrade
  • 346
  • 3
  • 13
  • The optimal approach for pagination is to query the data based on a proper index, and hold the cursor open. For each page, read the appropriate quantity of records from the cursor into a local variable for retention (Mongo cursors cannot be walked backwards). This can be challenging because many apps are stateless. In a highly concurrent system you might have multiple cursors, and/or local variables holding data. Several caveats - if data is added after the original query they will not be included in the cursor. MongoDB sessions and cursors time out. If they time out you need to resubmit – barrypicker Nov 23 '21 at 18:27
  • Are you using Spring Data MongoDB in a Micronaut service? – Jeff Scott Brown Nov 29 '21 at 20:55

0 Answers0