1

I have array of objects:

[
 {_id: "5ca2141da0106d1320c0ae32", detail: 1},
 {_id: "5ca2141da0106d1320c0ae33", detail: 3},
 {_id: "5ca2141da0106d1320c0ae34", detail: 3}
]

How i can update document by id and its value from array? I'm trying to do it this way:

arrayOfObjects.map(i => {
        ids.push(i._id);
        details.push(i.detail);
    });
Model.updateMany(
        { _id: { $in: ids } },
        { $set: { detail: details} },

should i get an index of $in: ids for my set operator?

2
  • Hi bulldojka, I didn't understand what is the expected outcome. Could you post what is the result you're expecting? Commented Jun 25, 2019 at 11:34
  • Hi. I wanted to update my documents by id in array. I thought it could be done with a "updateMany". But that's not how it works. Commented Jun 25, 2019 at 13:21

2 Answers 2

1

Here is your array.

var arrayofObjects = [
  {_id: "5ca2141da0106d1320c0ae32", detail: 1},
  {_id: "5ca2141da0106d1320c0ae33", detail: 3},
  {_id: "5ca2141da0106d1320c0ae34", detail: 3}
]

arrayOfObjects.forEach(function(obj) {
  Model.update({"_id": obj._id}, {"$set": {"detail": obj.detail}}, callback);
});

UpdateMany is db.Collection collection object. Refer link.

Sign up to request clarification or add additional context in comments.

Comments

1

Prior to Mongo version 4.2 you have to execute each update separately as they have different conditions, this is best done using bulkWrite in order to save up on network overhead, like so:

const input = [
    {_id: ObjectId("5ca2141da0106d1320c0ae32"), detail: 1},
    {_id: ObjectId("5ca2141da0106d1320c0ae33"), detail: 3},
    {_id: ObjectId("5ca2141da0106d1320c0ae34"), detail: 3}
];

const bulk = [];
input.forEach((datum) => {
    bulk.push(
        {
            updateOne: {
                "filter": {_id: datum._id},
                "update": {$set: {detail: datum.detail}}
            }
        }
    )
})

await db.collection.bulkWrite(bulk)

Version 4.2 introduced pipelined updates which allows us to use aggregation operators in the update body, now we can execute a single update and leverage this power, you can achieve the update in multiple ways here is one example:

const input = [
    {_id: ObjectId("5ca2141da0106d1320c0ae32"), detail: 1},
    {_id: ObjectId("5ca2141da0106d1320c0ae33"), detail: 3},
    {_id: ObjectId("5ca2141da0106d1320c0ae34"), detail: 3}
];

db.collection.updateMany(
    { _id: {$in: input.map(v => v._id )}},
    [
        {
            $set: {
                detail: {
                    $getField: {
                        field: "detail",
                        input: {
                            $first: {
                                $filter: {
                                    input: input,
                                    cond: {
                                        $eq: [
                                            "$$this._id",
                                            "$_id"
                                        ]
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    ])

Mongo Playground

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.