簡體   English   中英

Mongoose 更新內場

[英]Mongoose update inner field

我的 mongoDB 中有這樣的文件:

{
   id: my_id
   inner_foo : [
      {
         inner_id : id_A
         inner_field : field_A
      },
      {
         inner_id : id_B
         inner_field : field_B
      }
   ]
}

收到請求時,我得到了兩個 id,一個id (這里是my_id )和一個inner_id (這里例如id_A )。 如何使用 JS 和 mongoose 更新內部字段? 例如使用其inner_id包含對象的數組之一的inner_field字段

我想我不能使用findByIdAndUpdate ,因為我無法使用該方法直接引用正確的inner_foo條目,那么我該怎么做呢?

非常感謝。 凱夫。

演示 - https://mongoplayground.net/p/2pW4UrcVFYr

閱讀 - https://docs.mongodb.com/manual/reference/operator/update/positional/

db.collection.update(
    { id: "my_id", "inner_foo.inner_id": "id_A" }, // find the matching document
    { $set: { "inner_foo.$.inner_field": "id_A_Update" }} // set the value on the matched array index
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM