Trying to solve a conundrum I have in writing some queries.
I’ve traditionally used simple find operations (and cursor operations for paging functionalities), but need something more complex to address searching for all children and recursive sub-children, and the inherent performance issues of the cursor method for paging.
For context, I’m utilizing the Java driver, and typed collections mapping to pojo’s, but don't think it matters in this context.
Data model:
{ "_id": "<id>", "parent": "<nullable parent id of parent object in same collection>" ... others... } With a typical aggregate pipeline something like (not accurate to model above, just an example):
[ { $match: { company_id: ObjectId("someId") } }, { $sort: { order_number: -1 } }, { $setWindowFields: { output: { totalCount: { $count: {} } //attempt at total count } } }, { $skip: 20 }, //skip to beginning of page { $limit: 10 } //page size ] My intent is less to modify the data coming back and just have more control of the query process (ie, enable parent/children searches), and performance improvements with paging (skip and limit).
I have seen some examples for the parent-child queries using $graphLookup, but haven’t attempted yet, working out the basics first. If anyone has any suggestions or examples of this, I’m all ears! To re-iterate, I would like a query to find all children and sub-children given a parent.
The big issue I am having at the moment is determining how to get back a “total count in query”. There is obviously a window field set in the example, but it’s not apparent how to access this in the end, given the pojo mapping. Otherwise, I feel it’s fairly self-evident in the simple usecase to accomplish basic search and paging.
Is there a method I am missing? I feel like this should be a solved problem and perhaps the “modern” web is making it difficult to find.
tl:dr; How to use mongodb aggregate pipelines to perform performant paged searches, while also retrieving total query count. Bonus question of how to query "all results that are children of the given parent" in one query