3

I want to push one value in attachments array using mongodb. I want to update query using following criteria.

_id:ObjectId("5b56bd2f3e18580edc85af73") "cardID": ObjectId("5b56c895d0a04836f71aa776") "commentId":"2"

I want to push value in attachments, any help would be appreciated

This is a collection object:

{
            "_id" : ObjectId("5b56bd2f3e18580edc85af73"),
            "orgId" : "90",
            "createdBy" : "test",
            "name" : "testname",
            "Cards" : [ 
                {
                    "cardID" : ObjectId("5b56c895d0a04836f71aa776"),
                    "cardName" : "test Name",
                    "cardCreated" : "",
                    "reviewer" : "",
                    "priority" : "",
                    "cardPosition" : "",
                    "membersAssigned" : [ 
                        "ggg", 
                        "fff"
                    ],
                    "labels" : [ 
                        "l1", 
                        "l2"
                    ],
                    "description" : "",
                    "attachements" : [],
                    "comments" : [ 
                        {
                            "commentId" : "2",
                            "commentedBy" : "test",
                            "date" : "",
                            "comment" : "Hello world",
                            "attachements" : [   
                                "1", 
                                "data"
                            ],
                            "emojis" : [ 
                                ":smile:", 
                                ":thumbsup:"
                            ],
                            "updatedBy" : "arkadata",
                            "updatedOn" : "",
                            "subComments" : {
                                "commentedBy" : "jaril",
                                "date" : "",
                                "comment" : "Hello world inside dark"
                            }
                        }, 
                        {
                            "commentId" : "3",
                            "commentedBy" : "test",
                            "date" : "",
                            "comment" : "Hello world",
                            "attachements" : [ 
                               "1",
                               "raj"
                            ],
                            "emojis" : [ 
                                ":smile:", 
                                ":thumbsup:"
                            ],
                            "updatedBy" : "arkadata",
                            "updatedOn" : "",
                            "subComments" : {
                                "commentedBy" : "jaril",
                                "date" : "",
                                "comment" : "Hello world inside dark"
                            }
                        }, 
                        {
                            "commentId" : 6.0
                        }
                    ],
                    "dueDate" : "",
                    "createdDate" : "",
                    "lastUpdated" : "",
                    "checkList" : [],
                    "position" : "5",
                    "status" : "active"
                },
            "timestamp" : ISODate("2018-07-24T05:46:23.890Z")
        }
0

1 Answer 1

5

You can try with mongodb 3.6 arrayFilters

db.collection.update(
  { "_id": ObjectId(5b56bd2f3e18580edc85af73) },
  { "$push": { "Cards.$[card].comments.$[comment].attachments": "2" } },
  { "arrayFilters": { "card.cardID": ObjectId("5b56c895d0a04836f71aa776"), "comment.commentId": 2 } }
)

Make sure you cast your ids to ObjectId

Edit:

db.collection.update(
  { "_id": ObjectId(5b56bd2f3e18580edc85af73) },
  { "$push": { "Cards.$[card].comments.$[comment].attachments": "2" } },
  { "arrayFilters": [
    { "card.cardID": ObjectId("5b56c895d0a04836f71aa776")},
    {"comment.commentId": 2 }
    ]
  }
)
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.