Pass query criteria to mongoDB aggregation

Viewed 20

our current setup is: SPA frontend, Azure functions with mongoose middleware, MongoDB

(Maybe first read the question***)

Since we have a lot of documents in our DB and our customer wants to query them we are facing the following problem:

The user is assigned to his organization. He wants to search for Doc1s he has not responded to.

Doc1 { _id organization -> partitionKey content }

By creating doc2 with reference to doc1 he can respond.

Doc2 { _id organization -> partitionKey Doc1ref content }

We have a 1:n relationship.

At the moment we filter just by query criteria of doc1 with limit and skip options. But the new requirement is to filter the same way by referring doc2s.

I was thinking of:

  • Doing it in my code => Problem: after we have read with limit=100 and I filter it by my code, the result is not 100 anymore.

  • Extending doc1 by doc2 arrays => Must be the last option

  • Dynamic aggregation, Prepared in the code and executed at runtime => Don't want to user dynamic aggregations and the benefits of mongoose are almost lost.

  • Create a MongoDB view with lookup aggregation (populating doc1 by doc1.respondedOrganizations) => Problem is see here is the performance. When searching a lot of documents and then joining them by a non partitionKey.

*** So, I come to my question: Is it possible to pass a virtual (not existing) query criteria...

doc1.find({ alreadyResponded : my.organization } )

...and use it as input variable in an aggregation

{
  $lookup: {
    from: Doc2s,
    localField: _id,
    foreignField: Doc1ref,
    as: < output array field >
      pipeline: [{
          $match: {
            $organization: {
              $eq: $$alreadyResponded 

            }]
        }
      }

It would reduce query performance extremly.

Thanks

0 Answers
Related