3

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 ?

5
  • You need to use arrayFilters to update deeply nested array... If you can let me know which element from the array you want to update then I can suggest the script for it Commented Aug 6, 2018 at 11:38
  • Thanks for your response. I will check Commented Aug 6, 2018 at 11:40
  • how do i update the value? Commented Aug 6, 2018 at 11:42
  • what is your mongodb version? Commented Aug 6, 2018 at 12:29
  • My version is 3.6 Commented Aug 6, 2018 at 13:03

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.