Simply, I am working on a project and I faced a case and want to know if I am implementing it correctly.
I expect the client to send an array of IDs, and I need to check if each value in the array exists or not in the database. If any value does not exist, I will send a response with an error. Otherwise, I will proceed with the next actions.
I have implemented the idea in the following way:
let result = null;
for (let i = 0; i < listIds.length; i++) {
const existCheck = await branchModel.exists({ _id: listIds[i] }) != null ? true : false;
if (existCheck == false) {
result = false;
}
}
if (result != false) {
result = true;
}
return result;
}
However, I think this approach may impact performance if the array is very long, and I am not sure if calling the database multiple times in the same API is healthy and not expensive.
Is there a method in Mongoose to achieve the same idea?
2
Answers
Yes that will impact performance because as per the docs:
So if
listIds
is a long array then you are doing afindOne
once per each item in the array so that is one database call per array item.I have had to do something similar like this in the past and if you are only concerned with knowing that at least one
_id
is not there and not necessarily worried about which one then you can just get all documents with an$in
and then comparing the length of thelistIds
with the length of the documents. Given that_id
is unique, if they don’t match then at least one is not there.Or with using mongoose Query Builders:
Use
countDocuments
in Mongoose and filter by the_id
s inlistIds
.If the count of docs is not the same as the length of the list, then one or more objectIDs were missing. Add a step to make
listIds
unique. Otherwise,["aaa", "aaa", "aaa"]
would give an error since you’d get back only one document for one"aaa"
, ie count=1 even though the input was 3.And if listIds is are, convert them toEdit: This conversion is not needed since mongoose will do that automatically.ObjectIDs
before doingcountDocuments
.Note: It’s probably be better to use
find + length
as per jQueeny’s answer since it would use the index on_id
. But for countDocs: