如何在nodejs和mongoose中更新单个嵌入式文档

mf98qq94  于 2022-11-24  发布在  Go
关注(0)|答案(1)|浏览(126)

如何在nodejs和mongoose中更新文档内的单个嵌入文档

wpx232ag

wpx232ag1#

假设用户集合中有如下文档:

{
   _id: "joe",
   name: "Joe Bookreader",
   address: {
              street: "123 Fake Street",
              city: "Faketon",
              state: "MA",
              zip: "12345"
            }
}

您想更新子文档地址吗

var UserModel = require('path-to-user-mongoose-model-here');

UserModel.updateOne({_id:"joe"},
                    {$set:{
                           "address.street":"My Updated Street", 
                           "address.city":"My City",
                           "address.state":"NY",
                           "address.zip":"11332"
                         }
                    });

相关问题