Compare the attribute of a collection with a separate array of objects in MongoDB

Viewed 35

I have been stuck on this for a day now and Aggregation is not my strongest point. I have a very large array of data(say 10,000 objects) that I get from another endpoint in the form of:

let dataFromAPI = [
{
    _id: 61bb215de72ec72dfc08ccca,
    questionId: 61bb6de20630227c2bbcaf8a,
    questionStatus: "Open"
    __v: 0
  },
{
    _id: 61bb215de72ec72dfc08ccca,
    questionId: 61bb6de20630227c2bbcaf8b,
    questionStatus: "Open"
    __v: 0
  },
...
]

and I have another entity in my own database which stores this questionId as well. I want to compare the data based on this questionId.

await MyEntity.exists({
  questionId : // find the dataFromAPI._id here from the 'dataFromAPI' above
  questionStatus: "Closed"
})

If the data exists in this MyEntity, then I want to skip this question from sending in the res. Not only the exact query but any hint for the approach or a hint regarding what operator or what kind of aggregation to use would be really helpful.

Thanks.


Just a follow up

I have used the iteration of the data to match the ids of course. I am still looking for an optimized way of achieving this.

0 Answers
Related