I have this use case:
- Search the
Requestcollection based on the name of the user who submitted the request, and paginate the returned response
Unfortunately, the Request collection does not directly contain the username field. Instead, it contains the userId field which refers to a User document. That User document then contains the username field.
One approach that directly comes to mind is to embed a duplicate username field in the Request document that needs to be updated every time the corresponding User document's username field is updated.
Barring that approach, there are two other approaches I have thought of, but I am not sure what the implications are performance-wise.
- Approach 1: find the user IDs that match the name given in the query in the
Userdocument first, then use thatuserIdsas filter in theRequestcollection. The disadvantage of this is using the$inoperator and the$inoperator is recommended to only contain tens of values (which I cannot guarantee will be the case when I search get the user IDs in theUserdocument), that can negatively impact performance. - Approach 2: embed all the
Requestcollection with theusernamefield on each request using an aggregation pipeline, then search the embedded documents and apply pagination. I cannot find documentation on how performance is to embed a single string field to all the documents in theRequestcollection. I'm not sure if it is similar to SQL'sSELECT ... JOIN ... WHERE ...(in the SQL version, this is an acceptable query performance-wise afaik).
What are your thoughts on each of the approach I mentioned. Any docs mentioning the performance (something like the $in performance issue) is very welcomed.