简体   繁体   中英

Update a value in array of objects MongoDb

    {
      "name": "user2",
      "avatar": 1,
      "email": "example@gmail.com",
      "categories": [
        {
          "cname": "Category 1",
          "list": [
            {
              "status": "pending",
              "name": "List Item 1"
            },
            {
              "status": "pending",
              "name": "List Item 2"
            }
          ]
        }
      ]
    }

I want to update "categories.list.status" = "pending" to "completed". How can I do it? I tried using positional operator($) but it is giving error too many positional operator.

If you are using Mongoose (as your tags suggest), you can just update the value in the document object and then save it

document.list[0].status = "completed";
document.save();

I tried this. It worked.

    document.updateOne({_id: id}, {
    {
        $set: {
              "categories.$[].list.$[ele].status" : status
             }
    },
    {
        arrayFilters: [{"ele.name" : name}]
    }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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