1

I am trying to delete object 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",
                                "_id" :ObjectId("5b7bd0890394a62354a672cd")"
                                }, 
                                {
                                    "path" : "/uploads/inspection/00000000996b902b7c3f5efa/images/1533200242008-YN8IlA5yrMn3cBnn.png",
                                    "img_name" : "1533200242008-YN8IlA5yrMn3cBnn.png",
                                    "title" : "Box",
                                    "id" : "2",
                                "_id" :ObjectId("5b7bd0890394a62354a672ce")"

                                }
                            ],
                            "comment" : [ 
                                "comment4"
                            ],
                            "recommendation" : ""
                        }
                    ]
                }]
}

Now I want to remove this from this total object.

I tried this following code :

Updated :

Check this mongoose query:

mongo.inspection.update({"projectID" : mongoose.Types.ObjectId(req.body.projId) },
        { $unset: 
            { 'inspection_data.$[e1].locationAspects.$[e2].comments.$[e3].images': { _id : mongoose.Types.ObjectId(req.body.imgid) } } },
          { "arrayFilters": [
            { "e1._id": mongoose.Types.ObjectId(req.body.InspId)},
            { "e2._id": mongoose.Types.ObjectId(req.body.aspectId)},
            { "e3._id": mongoose.Types.ObjectId(req.body.commentId)},
            // { "e4.id": imgid } 
          ]},
          function(err,response){
              if(err){
                  console.log(err)
              }
              else{
                console.log("ok")

              }
            })

Am getting ok in console but my image object is not get removed.I tried $pull too. How do i delete the object inside Array .

7
  • 1
    this will work stackoverflow.com/questions/51706647/… Commented Aug 22, 2018 at 7:15
  • Ya Its working thank you. But its deleting both the images Commented Aug 22, 2018 at 7:26
  • could you post your query Commented Aug 22, 2018 at 7:29
  • 1
    Possible duplicate of Update deeply nested array in mongodb Commented Aug 22, 2018 at 7:38
  • 1
    Yes its working... Thank you so much Commented Aug 22, 2018 at 7:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.