Zamurph
Zamurph

Reputation: 89

Is there a way to update multiple documents with different values in mongoose?

I'm feeling like there probably isn't a way to do this, but here's what I want to accomplish, I have an array of products to update like so:

const productsToUpdate = [
    {
        "imgUrls": {
            "base": [
                "https://files.cdn.printful.com/files/ba1/ba13faa1332b7f18ec847cb9f4d79868_preview.png"
            ],
            "side": [
                "https://files.cdn.printful.com/files/ba1/ba13faa1332b7f18ec847cb9f4d79868_preview.png"
            ]
        },
        "_id": "5dd6173cf50c1d1a40fe7c2c",
        "category": "MENS",
        "name": "JavaScript is Cool",
        "price": "27.50" 
    },
        {
        "imgUrls": {
            "base": [
                "https://files.cdn.printful.com/files/ba1/ba13faa1332b7f18ec847cb9f4d79868_preview.png"
            ],
            "side": [
                "https://files.cdn.printful.com/files/ba1/ba13faa1332b7f18ec847cb9f4d79868_preview.png"
            ]
        },
        "_id": "5dd6173cf50c1d1a40fe7c2c",
        "category": "MENS",
        "name": "Testing",
        "price": "25.50" 


    }
]

This data structure matches the data of products currently stored in my database. Basically, I need to find a current product by _id and be able to update either the category, price, imgUrls, or name or all of the above.

I've done some research and know I can grab all the ids from the toUpdate arr and then do something like

Collection.update({ _id: { $in: arrOfIds }, {}, {multi: true}))

But I'm not sure how to fill in the query to have the correct category/price/name etc.

I would greatly appreciate any guidance or if you need more information please let me know!

Upvotes: 1

Views: 169

Answers (1)

mickl
mickl

Reputation: 49945

The "mass-update" or rather the most efficient way of that kind of updates is called bulkWrite. Since there are is more fields in your database and you don't want to lose them you need to use $set operator.

const productsToUpdate = [
    {
        "imgUrls": {
            "base": [
                "https://files.cdn.printful.com/files/ba1/ba13faa1332b7f18ec847cb9f4d79868_preview.png"
            ],
            "side": [
                "https://files.cdn.printful.com/files/ba1/ba13faa1332b7f18ec847cb9f4d79868_preview.png"
            ]
        },
        "_id": "5dd6173cf50c1d1a40fe7c2c",
        "category": "MENS",
        "name": "JavaScript is Cool",
        "price": "27.50" 
    },
        {
        "imgUrls": {
            "base": [
                "https://files.cdn.printful.com/files/ba1/ba13faa1332b7f18ec847cb9f4d79868_preview.png"
            ],
            "side": [
                "https://files.cdn.printful.com/files/ba1/ba13faa1332b7f18ec847cb9f4d79868_preview.png"
            ]
        },
        "_id": "5dd6173cf50c1d1a40fe7c2c",
        "category": "MENS",
        "name": "Testing",
        "price": "25.50" 


    }
];

let toUupdate = product => ({
    updateOne: { 
        "filter": { "_id": product._id }, 
        "update": { "$set": { category: product.category, name: product.name, price: product.price }  }   
    }
})

db.collection.bulkWrite(productsToUpdate.map(toUupdate);

console.log(updates);

Upvotes: 1

Related Questions