Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

html - 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...