Reputation: 1133
Let say i have the following array:
[
{_id: 1, status: "Active"},
{_id: 2, status: "Delete"},
{_id: 10, status: "Pause"},
{_id: 20, status: "Active"}
]
And i want to update the field status
in collection users
with a single mongoDB call using mongoose. (Update only documents with _id = 1,2,10,20)
My Schema can look like this:
var userScheme = new Schema({
fname : String,
lname : String,
status : String,
email : String
});
db.model('user', userScheme);
NOTE: I want to use updateMany
and not update
Upvotes: 2
Views: 500
Reputation: 1133
For those of you who will want a complete example instead of being re-route to other question, here is my answer which is based on @Ashh 's answer.
db.model("user").bulkWrite(
usersArray.map((data) =>
({
updateOne: {
filter: { _id: data._id },
update: { $set: {status: data.status} }
}
})
)
);
Upvotes: 2