简体   繁体   English

猫鼬更新多个文档上的嵌套对象

[英]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'. 假设我们有10个具有该模式的文档,其中5个具有postalCode'12345'。

How can I select all of them and update the extraInfo.phone and extraInfo.age of these documents? 如何选择所有这些文件并更新这些文档的extraInfo.phone和extraInfo.age?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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