I want to find docs with an array of ids. I have two way to do that
using find({_id:{$in:arr}} , callback)
using multiple findById, and use some async code wrapper to execute them together.
What I concern here is which one is better? For the server side seems the second needs more computing, but what about the database part?
Thx!
It depends on what you consider is a heavy operation for your system. Option 1 requires 1 DB call where as Option 2 requires several (depending on how many you fetch).
Anyways, option 1 seems to be a better option since it would return a cursor and you'd just be iterating over it. Where as for option 2 you'd need multiple db calls (probably be doing it in a loop) and it would be returning the data and closing the cursor.
The only time option 2 would benefit you is when you have just one value to find or you have special indexes and not just $in queries. Say index on attribute1, attribute2.
You can refer to this.