简体   繁体   中英

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?

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

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