0

I am trying to update nested field of a ducument, but don't why it is not getting succeeded. Please help.

My document structure is like :

{
    "_id": {
        "$oid": "594d173df36d285a33da90cf"
    },    
    "available": 1,
    "brand": [
        {
            "_id": {
                "$oid": "59db10918bbe2316281d58d1"
            },
            "name": "test",            
            "createdby": "***************",
            "createdon": "Mon Oct 09 2017 11:30:42 GMT+0530 (India Standard Time)",
            "feedbacks": [
                {
                    "_id": {
                        "$oid": "5a1039c25443604328ecf398"
                    },                    
                    "feedback": "hello",
                    "date": "Sat Nov 18 2017 19:16:36 GMT+0530 (India Standard Time)",
                    "reportedby": []
                },
                {
                    "_id": {
                        "$oid": "5a1039c25498504328ecf398"
                    },                    
                    "feedback": "hello",
                    "date": "Sat Nov 18 2017 19:16:36 GMT+0530 (India Standard Time)",
                    "reportedby": []
                }
            ]
        }
    ]
}

And my query is like :

db.products.update({
                    '_id': mongojs.ObjectId(594d173df36d285a33da90cf),
                    'brand._id': mongojs.ObjectId(59db10918bbe2316281d58d1),
                    'brand.feedbacks._id': mongojs.ObjectId(5a1039c25498504328ecf398),
                }, {
                    $addToSet: { 'brand.feedbacks.reportedby': skillSet.userId }
                },
                function(err, products) {

                })

The error is like : { n: 0, nModified: 0, opTime: { ts: Timestamp { bsontype: 'Timestamp', low: 1, high_: 1512829477 }, t: 5 }, electionId: 7fffffff0000000000000005, ok: 1 }

kmrrakesh
  • 137
  • 1
  • 1
  • 7
  • Which version of MongoDB are you using? – dnickless Dec 09 '17 at 14:37
  • @dnickless it is 3.4 – kmrrakesh Dec 09 '17 at 14:40
  • This cannot be done on the server side in 3.4. Starting with 3.6 you can do it, though: https://jira.mongodb.org/browse/SERVER-1243. You will need to either upgrade or load the document, do the change on the client side and then save it to MongoDB. – dnickless Dec 09 '17 at 14:41
  • Possible duplicate of [Updating nested arrays in mongoDB via mongo shell](https://stackoverflow.com/questions/18573117/updating-nested-arrays-in-mongodb-via-mongo-shell) – dnickless Dec 09 '17 at 14:44

1 Answers1

0

You can try this query:

{brand:{$addToSet:{'feedbacks.reportedly':'skillset.userId}}}
jazeb007
  • 578
  • 1
  • 5
  • 11