Ashutosh Soni
Ashutosh Soni

Reputation: 1025

Update many on all the documents in mongodb

So I have a collection "services". Which has a json field like this

"price" : {
    "_id" : ObjectId("5e82d0d1f44d762a248aab38"),
    "junior" : {
        "_id" : ObjectId("5e82d0d1f44d762a248aab39")
    },
    "senior" : {
        "_id" : ObjectId("5e82d0d1f44d762a248aab3a")
    }
},

But I don't care what is there inside the price values. I need to update it everywhere inside my collection with the following price key. how can I do that? I serched For updateMany but it requires a value field. like db.test.updateMany({foo: "bar"}, {$set: {test: "success!"}}). here I don't have the "bar". I just want to do it in everywhere inside my collection with the following field.

"price" : {
        "_id" : "5e2e77ddac85aa32f8cd840b",
        "junior" : {
            "atSpa" : 895.24,
            "prePay" : 675.52,
            "offer" : {
                "atSpa" : 204.08,
                "prePay" : 764.9
            }
        },
        "senior" : {
            "atSpa" : 1895.24,
            "prePay" : 1675.52,
            "offer" : {
                "atSpa" : 1204.08,
                "prePay" : 1764.9
            }
        },
        "offerConditions" : {
            "noOfTimes" : 4,
            "dueDate" : "2020-03-27T05:40:36.652Z"
        }
    }, 

Upvotes: 0

Views: 827

Answers (1)

als
als

Reputation: 163

updateMany takes three arguments: A filter, an update document and an optional options object. Leaving the filter empty:

db.test.updateMany({}, {$set: {test: "success!"}})

will select everything for the update.

Upvotes: 2

Related Questions