I need some help with the following requirement, how would you do this in one batch, if this even possible in mongodb with nodejs?
I got multiple array's of 1000 id's from another source via the API. I need to get a result for each id.
Basically it should look like this:
const bulk = db.collection('profiles').initializeUnorderedBulkOp();
arrayWith1000TagIds.forEach((id) => {
bulk
.find({ tags: { $elemMatch: { _id: new ObjectId(id) } } })
.count();
Unfortunately count
is not a function of bulk. Any other way of doing this in one batch instead of doing a query for every id? In the end this is going to be 10k of queries.
Thank you. I'm pretty new to mongodb.
The result should look like this:
{
[tagId]: 3, (There are 3 profiles, which include this specific tag in their tag list)
[tagId]: 2,
[tagId]: 0,
...
}