1

I am trying to write a generic query to update the attempts1 field

{
    "_id" : ObjectId("59d659eb9d492019f018cf14"),
    "_class" : "com.anthem.twofatoggleattemptevent.mongo.collections.abc",
    "masterCollection1" : {
        "version" : "1.0",
        "keys" : [ 
            {
                "type" : "test",
                "value" : "636T90012~1985-01-01~Mohan~test"
            }, 
            {
                "type" : "usernm",
                "value" : "~AF99990865"
            }, 
            {
                "type" : "webguid",
                "value" : "25e03606-da67-448a-8ece-881829b95daa"
            }
        ],
        "events" : [ 
            {
                "_id" : ObjectId("59d659eb9d492019f018cf13"),
                "eventType" : {
                    "code" : "Test-UAT",
                    "desc" : "Test"
                },
                "eventSrc" : "Toggle_Indvidual",
                "suggestedAction" : "Test",
                "effectiveDt" : ISODate("2017-01-01T05:00:00.000Z"),
                "terminationDt" : Date(253402232400000)
            }, 
            {
                "_id" : ObjectId("59d65a1a9d492019f018cf16"),
                "eventType" : {
                    "code" : "Test-UAT",
                    "desc" : "Test"
                },
                "eventSrc" : "Toggle_Indvidual",
                "suggestedAction" : "Test",
                "effectiveDt" : ISODate("2017-01-01T05:00:00.000Z"),
                "terminationDt" : Date(253402232400000)
            }, 
            {
                "_id" : ObjectId("59d65ac49d492019f018cf18"),
                "eventType" : {
                    "code" : "Test-UAT",
                    "desc" : "Test"
                },
                "eventSrc" : "Toggle_Indvidual",
                "suggestedAction" : "Test",
                "effectiveDt" : ISODate("2017-01-01T05:00:00.000Z"),
                "terminationDt" : Date(253402232400000)
            }, 
            {
                "_id" : ObjectId("59d65b8d9d492019f018cf1a"),
                "eventType" : {
                    "code" : "Test-UAT",
                    "desc" : "Test"
                },
                "eventSrc" : "Toggle_Indvidual",
                "suggestedAction" : "Test",
                "effectiveDt" : ISODate("2017-01-01T05:00:00.000Z"),
                "terminationDt" : Date(253402232400000)
            }, 
            {
                "_id" : ObjectId("59d65d309d492019f07817fd"),
                "eventType" : {
                    "code" : "Test-UAT",
                    "desc" : "Test"
                },
                "eventSrc" : "Toggle_Indvidual",
                "suggestedAction" : "Test",
                "effectiveDt" : ISODate("2017-01-01T05:00:00.000Z"),
                "terminationDt" : Date(253402232400000)
            }
        ],
        "attempts1" : [ 
            {
                "_id" : ObjectId("59dbbdb2c6700e1b08261059"),
                "type" : "001",
                "desc" : "Defer",
                "count" : 1,
                "srcType" : "Login-contactUpdate"
            }, 
            {
                "_id" : ObjectId("59dbc884c6700e1b08b571d4"),
                "type" : "002",
                "desc" : "Confirm",
                "count" : 1,
                "srcType" : "Login-contactUpdate"
            }, 
            {
                "_id" : ObjectId("59dbcc8bc6700e1b08b571d7"),
                "type" : "003",
                "desc" : "Update",
                "count" : 1,
                "srcType" : "Login-contactUpdate"
            }
        ]
    }
}

The below query works for me but I want a complete query for this job.

update({"masterCollection1" : {$exists: true}},{$set: { "masterCollection1.attempts1.0.type" : "444444" }}, {multi: true }
user2316896
  • 29
  • 1
  • 6
  • What exactly do you want to update? The first element in the `attempts1` array? Can you please provide the smallest amount of sample code possible plus your desired result? – dnickless Oct 16 '17 at 18:55
  • Yes, want update first element of attempts1 means 0.type – user2316896 Oct 16 '17 at 19:23
  • So what exactly is it that you would want to be generic? I don't understand what your question really is... – dnickless Oct 16 '17 at 19:39
  • Have to write a update query to update data under the attempts1 fields below query is work for me but want proper query . update({"masterCollection1" : {$exists: true}},{$set: { "masterCollection1.attempts1.0.type" : "444444" }}, {multi: true } – user2316896 Oct 17 '17 at 08:04
  • What is a "proper" query? What's wrong with this query? – dnickless Oct 17 '17 at 08:29
  • Possible duplicate of https://stackoverflow.com/questions/4669178/how-to-update-multiple-array-elements-in-mongodb ? – CS_noob Oct 17 '17 at 13:30
  • @dnickless, I feel, the OP wants to update all subdocuments inside attempts1 with value 444444. At present, he has mentioned as ` { "masterCollection1.attempts1.0.type" : "444444" }` He wants it to work with mentioning the index i.e `attempts1.0`. – CS_noob Oct 17 '17 at 13:32

0 Answers0