Is embedding a field using aggregation pipeline before searching and implementing pagination performant in Mongoose?

21 Views Asked by At

I have this use case:

  • Search the Request collection 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.

  1. Approach 1: find the user IDs that match the name given in the query in the User document first, then use that userIds as filter in the Request collection. The disadvantage of this is using the $in operator and the $in operator is recommended to only contain tens of values (which I cannot guarantee will be the case when I search get the user IDs in the User document), that can negatively impact performance.
  2. Approach 2: embed all the Request collection with the username field 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 the Request collection. I'm not sure if it is similar to SQL's SELECT ... 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.

0

There are 0 best solutions below