Home > OS >  How do I match by two separate arrays of ids in MongoDB?
How do I match by two separate arrays of ids in MongoDB?

Time:07-06

I have 2 arrays with different ids :

  1. bikesWithNoOrders [id , id1 , id2]
  2. filteredResult [id3 , id5]

How do I make a query that finds them all

I have this :

queryBuilder.find({ _id: { $in: bikesWithNoOrders } });

queryBuilder.find({ _id: { $in: filteredResult } });

But the second one overwrites the first one because they dont share the same ids I want something like this :

queryBuilder.find({ _id: { $in: filteredResult } }).or({ _id: { $in: bikesWithNoOrders } });

CodePudding user response:

You can just marge these 2 arrays on server side, and query with merged array. You can do it with concat():

queryBuilder.find({ _id: { $in: bikesWithNoOrders.concat(filteredResult) } });

CodePudding user response:

Hope this answer will work for you.!

queryBuilder.find({ _id: { $in: [...bikesWithNoOrders,...filteredResult] } });
  • Related