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")
 }
Ashh
46.6k16 gold badges111 silver badges137 bronze badges
asked Jul 24, 2018 at 9:54
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 }
 ]
 }
)
answered Jul 24, 2018 at 10:10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.