Suppose we have this array:
const array = [{ code:1, pw:'abc'}, { code:2, pw:'grt'}, { code:3, pw:'tpo'}, { code:4, pw:'xyz'}]
and we have these docs in our db from model called User:
[{ code:1, pw:'___'}, { code:2, pw:'___'}, { code:3, pw:'___'}, { code:4, pw:'___'}]
What’s the most efficient way you’d suggest to update the pw fields from db with pws from the array at one shot (in Mongoose)? (we definitely want the codes from both arrays to match) Thank you.
2
Answers
A simple and efficient option will be to use a bulk:
It can also be done in an update with pipeline query:
See how it works on the playground example
But I think it might be less efficient than a bulk update.
You can do it like this:
Working example