Home > Software design >  Return empty array for ids not found in mongoose find by array of ids
Return empty array for ids not found in mongoose find by array of ids

Time:05-04

So what I am trying to do is find all objects whose ids are in the array I am providing and that part is working fine.

But I need it to return an empty array if it does not find any object with the id I am also not using the _id to find I am using a normal field

e.g [{find},{find},[],{find},[],[find]...]

CodePudding user response:

There is no capability provided in the query language to do this.

This is something that might be useful to know, but in general this association will need to be performed by the client.

This would allow the client to decide whether it is worth submitting a separate query for each element to ensure matching, and handle any potential duplicate returns; or the client could match the result array with the input array to make the association with a single query.

I don't think there will be a single always-best method to do this, so it is probably best left to the application developer to decide.

  • Related