How to execute bulk fetch query in mongodb

I have Q1,Q2,Q3,Q4,…Qn queries.

Need to execute all the find queries in a single operation and need resultset separately for each queries

Ex: Just an assumption

and output
Q2: ,
Qn: }

Hi, I am not sure if this can be done directly, but there maybe a workaround. Not sure why you want it, as it will have a bad influence on the performance. You can try to write an aggregation query with OR of all queries, like (Q1 OR Q2 OR Q3) in a match stage, then apply $facet to break it into individual result sets. and get data.

Again, this is a very bad way to accomplish something you are asking, as each query needs to be performed separately, having its own result set. Not seeing how bulk queries can get something meaningful out.

1 Like

Hello : )

As been said this is what $facet does,but $facet will not execute each query in parallel,but serial.Also if you want all results to fit in 1 document,this document final size must be<16 MB.

$facet would make sense i think if you had like a common part that could be shared from all queries,before entering the $facet stage,to avoid doing that part many times for each “query”.

If the queries don’t share a common part,i think $facet will cause only restrictions and slower
execution time,from calling the n queries separately,so they can run in parallel,if you call them
in parallel (async or threads)

See this also