I would like to query for the students satisfying class = 3, odererd by order and return only the top 2. So I am looking for the following result:
Is that possible using the current api? If so, how?
Thanks for your help!
You can query for the “students” array filtered by “class” using a query similar to one below (DB021) but currently there isn’t a way to unnest /flatten the nested array in order to apply the orderBy on the members of the nested array. (at least none that I can think of)
let VAR_STUDENTCLASS = ArrayExpression.variable("student.class")
let searchQuery = Query
thanks for taking the time to answer my question and for clarifying the matter!
Unfortunately it is not the answer I was hoping for
The number of items in the nested array (the ‘students’) can get pretty large, so I was hoping I could only look at the top items, say top 10, and fetch more from the database only if and when needed and explicitly requested.
So as far as I can see, that leaves me with either the choice of always loading the complete nested array or changing the data model to one using reference instead of embedding.
Oh well, I’ll figure something out.
Nonetheless, thanks again for your help!
Yeah …If the array is expected to be very long then I would recommend modeling the data to use the “reference” model/ normalization . The alternative would be to fetch the students array and use language features like map / filter to extract relevant elements .
If it’s enough to worry about performance, they should probably be separate documents, as Priya said. But this sounds a bit like premature optimization. Have you tested performance and found it too slow?
you’re absolutely correct, it is premature. That’s why we decided to worry about it later, if and when it becomes a real problem and not just a theoretical one.
No, we have not tested performance and it might indeed be a while before this becomes a real issue. It just seems so ‘wasteful’ to always load thousands of items when you hardly ever need more than 20. But that’s more of an emotional issue, I guess.
Once it does indeed become a real problem, then there is always the alternative of changing the datamodel. So that’s fine.
Thanks to you both for taking the time to discuss this with me!