最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何从节点js中的数组(猫鼬)中查找和更新特定对象

IT培训 admin 12浏览 0评论

如何从节点js中的数组(猫鼬)中查找和更新特定对象

我想使用Arr_id从对象数组(myArr [])更新'Name'值。

mydb:{
     "_id" : ObjectId("5eb2b06d626fc539172013f8"),
     "is_deleted" : false,
     "email" : "[email protected]",
     "myArr": [
         {"Arr_id":"5eb2b06d626fc539172013f7",
          "Name":"Aman"
          },
         {"Arr_id":"5eb2b06d626fc539172001k9",
          "Name":"Ram"
          },
         {"Arr_id":"5eb2b06d626fc539172013k4",
          "Name":"Piyush"
          }
        ]
     }
回答如下:
You can update in nested objects using $ positional operator.
here , Id is the id you want to update the value of name.
db.collection.update(
    { "myArr.Arr_id": Id }, 
    { "$set": { "myArr.$.Name": "SHYam" } }

如何从节点js中的数组(猫鼬)中查找和更新特定对象

我想使用Arr_id从对象数组(myArr [])更新'Name'值。

mydb:{
     "_id" : ObjectId("5eb2b06d626fc539172013f8"),
     "is_deleted" : false,
     "email" : "[email protected]",
     "myArr": [
         {"Arr_id":"5eb2b06d626fc539172013f7",
          "Name":"Aman"
          },
         {"Arr_id":"5eb2b06d626fc539172001k9",
          "Name":"Ram"
          },
         {"Arr_id":"5eb2b06d626fc539172013k4",
          "Name":"Piyush"
          }
        ]
     }
回答如下:
You can update in nested objects using $ positional operator.
here , Id is the id you want to update the value of name.
db.collection.update(
    { "myArr.Arr_id": Id }, 
    { "$set": { "myArr.$.Name": "SHYam" } }

发布评论

评论列表 (0)

  1. 暂无评论