簡體   English   中英

如何更新 mongoose 中對象數組中的特定字段

[英]How to update the speicific field in array of objects in mongoose

您好,我想更新消息中的狀態字段。 我正在嘗試使用

db.messagev2.updateMany(
  {
    user1: 8, user2:150,
   "messages.receiver":150,
   "messages.status" : "sent",
    },
{'$set': {'messages.$.status': "read"}},{multi:true}) 

但只有消息中的第一個狀態正在更新..需要幫助在此處輸入圖像描述

您需要更換:

  'messages.$.status' 

和:

  'messages.$[elem].status'

並添加 $arrayFilters:

    { arrayFilters: [ { "elem.receiver": 150 ,"elem.status": "sent"} ] }

暫無
暫無

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

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