简体   繁体   中英

Update deeply nested array in mongodb

I am trying to update field value in mongoose.

{
    "_id" : ObjectId("5b62c772efedb6bd3f0c983a"),
    "projectID" : ObjectId("0000000050e62416d0d75837"),
    "__v" : 0,
    "clientID" : ObjectId("00000000996b902b7c3f5efa"),
    "inspection_data" : [ 
        {
            "pdf" : null,
            "published" : "N",
            "submissionTime" : ISODate("2018-08-02T08:57:08.532Z"),
            "userID" : ObjectId("00000000cac68e3bc04643f7"),
            "insSummary" : "inspected areas",
            "insName" : "Infotech",
            "_id" : ObjectId("5b62c772fa02622a18655e7b"),
            "published_date" : ISODate("2018-08-02T08:57:22.041Z"),
            "locationAspects" : [ 
                {
                    "aspectname" : "Ground floor",
                    "_id" : ObjectId("5b62c772fa02622a18655e80"),
                    "comments" : [ 
                        {
                            "_id" : ObjectId("5b62c772fa02622a18655e81"),
                            "images" : [ 
                                {
                                    "path" : "/uploads/inspection/00000000996b902b7c3f5efa/images/1533200242005-IpjLKH4XFWNEcHXa.png",
                                    "img_name" : "1533200242005-IpjLKH4XFWNEcHXa.png",
                                    "title" : "Fan",
                                    "id" : "1"
                                }, 
                                {
                                    "path" : "/uploads/inspection/00000000996b902b7c3f5efa/images/1533200242008-YN8IlA5yrMn3cBnn.png",
                                    "img_name" : "1533200242008-YN8IlA5yrMn3cBnn.png",
                                    "title" : "Box",
                                    "id" : "2"
                                }
                            ],
                            "comment" : [ 
                                "comment4"
                            ],
                            "recommendation" : ""
                        }
                    ]
                }]
}

Here I want to update a title Fan in image array as table fan.

I tried $set but I don't know how to do for my db structure.

Kindly give some solution to this

**Updated:**

I tried this code:

mongo.inspection.update({"projectID" : mongoose.Types.ObjectId(req.body.project_id) },
{ "$set": {

    "inspection_data.$[e1].locationAspects.$[e2].comments.$[e3].images.$[e4].title" : "TableFan"
  }},
  { "arrayFilters": [
    { "e1._id": mongoose.Types.ObjectId(req.body.insId)},
    { "e2._id": mongoose.Types.ObjectId(req.body.aspectId)},
    { "e3._id": mongoose.Types.ObjectId(req.body.commentId)},
    { "e4.id": "1" } 
  ]},function(err,response){
      if(err){
          console.log("error")
      }
      else{
          console.log('Updated')
          console.log(response)
      }
    })

db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )

Its showing updated but in my db there is no change. Is any mistake I did ?

You can try with arrayFilters in mongodb

var mongoose = require('mongoose')


Temp.update(
  { "_id" : mongoose.Types.ObjectId("5b62c772efedb6bd3f0c983a") },
  { "$set": {
    "inspection_data.$[e1].locationAspects.$[e2].comments.$[e3].images.$[e4].title": "TableFan"
  }},
  { "arrayFilters": [
    { "e1._id": mongoose.Types.ObjectId("5b62c772fa02622a18655e7b") },
    { "e2._id": mongoose.Types.ObjectId("5b62c772fa02622a18655e80") },
    { "e3._id": mongoose.Types.ObjectId("5b62c772fa02622a18655e81") },
    { "e4.id": "1" }
  ]}
)

Note : You have to cast _id to ObjectId

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