Stathis Ntonas
Stathis Ntonas

Reputation: 1262

Mongoose update nested objects on multiple documents

Here is the schema:

var user = new Schema ({
    name: {type:String},
    lastName:  {type:String},
    extraInfo: {
        phone: {type:String},
        age: {type: Number}
    },
    postalCode: {type:Number}
})

Let's assume we have 10 documents with that schema and 5 of them have postalCode '12345'.

How can I select all of them and update the extraInfo.phone and extraInfo.age of these documents?

Upvotes: 1

Views: 1010

Answers (1)

Rainer Plumer
Rainer Plumer

Reputation: 3753

Well, i dont have a database at hand, to try it out, but it should be something like this.

db.User.update(
    {postalCode: 12345}, 
    {"extraInfo.phone": value1, "extraInfo.age": value2},
    {multi: true});

Upvotes: 2

Related Questions