简体   繁体   中英

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!

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM