简体   繁体   English

如何更新嵌套对象? 猫鼬

[英]How to update nested object? Mongoose

I'm trying to update auction.auctionDescription .我正在尝试更新auction.auctionDescription After the request is sent auction.auctionDescription is updated, but auction.auctionImage is deleted.请求发送后, auction.auctionDescription被更新,但auction.auctionImage被删除。

await Auction.updateOne({_id: id} ,{auction: {'auctionDescription': auctionDescription}})

My document looks like the following:我的文档如下所示:

_id: ObjectId("...")
user: "..."
animal: {...}
auction: {
  auctionImage: "path/to/image.jpeg"
  auctionDescription: "..."
}

The way you currently have it coded, you are targeting the entire auction property and replacing it with the value {"auctionDescription": auctionDescription} , which is why it's overwriting the entire existing object.按照您目前的编码方式,您将整个auction财产作为目标,并将其替换为{"auctionDescription": auctionDescription} ,这就是它覆盖整个现有对象的原因。

if you are just trying to update a property on an object on a document, you can use "dot notation" to target the specific property on the object you are trying to update, like so:如果您只是想更新文档上对象的属性,则可以使用“点符号”来定位您尝试更新的对象上的特定属性,如下所示:

await Auction.updateOne({_id: id}, {"auction.auctionDescription": auctionDescription})

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

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