2

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

1

1 Answer 1

2

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} }
                        }
                    })
                )
            );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.