set/rename won’t work with arrays & sub-documents.

How can I update a field with another field value in the same document (in both array & embedded sub documents case)
From the below mentioned JSON document, I would like to update ‘analyticUsers.geid’ to : ‘analyticUsers._id’ & tried all the below possible queries RENAME/SET/UNSET- but doesn’t work ?

db.iainproposals.update(
{“_id” : 679}
, { $rename: {‘analyticUsers.geid’ : ‘analyticUsers._id’}})

db.iainproposals.update(
{“_id” : 679}
, { $set : { analyticUsers._id : analyticUsers.geid }})

db.iainproposals.update(
{“_id” : 679}
, { $unset : { analyticUsers.geid }})

db.iainproposals.update(
{“_id” : 679}
, { $set : { engagementManager._id : engagementManager.geid }})
Sample Document :

===================================================================================================================================================================================
{
“_id” : 679,
“analystResources” : [],
“analyticToolsTechniques” : “- xxxxxxxxxxxs”,
“analyticToolsTechniquesReuse” : “dbfry.com”,
“analyticUsers” : [
{
“id” : 986820,
“soeid” : “xxxx”,
“ritsid” : “1010004886”,
“name” : “xxxx”,
“manager” : null,
“emailaddress” : “abc123@dbfry.com”,
“officephone” : “xxxxx “,
“description” : null,
“isenabled” : “Y”,
“geid” : “xxxxxxxx”
}
],
“applicabilityReusability” : “High – xxxxxxx”,
“approvalStage” : “Review”,
“approvalStatusComment” : null,
“approvalStatusCommentCount” : 0,
“benefitRealization” : “3 to 6 Months”,
“category” : “Project”,
“charterApprovalTargetDate” : null,
“dbfry.comDataEngineer” : null,
“cluster” : “Burrito”,
“dataAcquisitionContacts” : [],
“dataGovernance” : [
{
“id” : 962963,
“soeid” : “xxxx”,
“ritsid” : “xxx”,
“name” : “xxxx”,
“manager” : null,
“emailaddress” : “123.abc@dbfry.com”,
“officephone” : “xxxxx “,
“description” : null,
“isenabled” : “Y”,
“geid” : “xxxxxxxxxxxxx”
}
],
“dataInnovationQualifiers” : [
“Use of cross-sector data”,
“Use of unstructured data”,
“Use of large volume of data”,
“Use of new technology”
],
“description” : “xxxxxxxxxxx.”
“engagementManager” : {
“id” : 1.13534e+006,
“soeid” : “xxx”,
“ritsid” : “xxx”,
“name” : “xxxxx”,
“emailaddress” : “ron.xyz@dbfry.com”,
“officephone” : “+1 (212) “,
“description” : null,
“isenabled” : “Y”,
“geid” : “xxxxxxxxxxxxxx”,
“manager” : null,
“_id” : “engagementManager.geid”
},
“engineeringStatusNotes” : null,
“engineeringStatusNotesCount” : 0,
“executiveSponsor” : {
“id” : 908763,
“soeid” : “xxxx”,
“ritsid” : “xxxxxx”,
“name” : “xxx-xxxx”,
“emailaddress” : “abc.curry@test.in”,
“officephone” : “+1 (xx)xxxxxx “,
“description” : null,
“isenabled” : “Y”,
“geid” : “xxxxx”,
“manager” : null
},
“expiryDate” : ISODate(“2014-12-30T05:00:00.000Z”),
“forEngineerUse” : false,
“funding” : “No”,
“indicatorMeasures” : “1) Higher online conversion rate 2) Incremental Revenue”,
“keyGoalsDescription” : “”,
“lengthofAccess” : “> 6 months”,
“lob” : “GDM”,
“objectives” : [
“Risk Reduction & Control Improvement”,
“Customer Experience Improvement”
],
“owningOrganization” : “Consumer”,
“pmoLevel” : “”,
“potentialAdverseImpacts” : “The dataanalysis and treatment”,
“primaryUnixGroup” : null,
“projectExecutionStatus” : “N/A”,
“projectManager” : null,
“projectType” : “Analytic”,
“sector” : “GCG”,
“submittedBy” : {
“id” : 1.12006e+006,
“soeid” : “xxxx”,
“ritsid” : “x”,
“name” : “xxxxx”,
“emailaddress” : “abcd.xyz.in”,
“officephone” : “XXXXXXXXXXX “,
“description” : null,
“isenabled” : “Y”,
“geid” : “xxxxxxx”,
“manager” : null
},
“submittedDate” : ISODate(“2013-01-07T05:00:00.000Z”),
“title” : “Digital Conversion Analysis for Consumer – International”,
“unixFunctionalId” : null,
“updatedDate” : ISODate(“2013-10-10T22:30:35.359Z”)
}

===================================================================================================================================================================================
Answers :
===================================================================================================================================================================================

The $rename option only works for single fields and embedded documents. The $rename option is not available for documents in an array.
There is however a way to update the fields in the array. You could do something like this:

db.iainproposals.find({“_id”: 679}).forEach(function(item)
{
for(i = 0; i != item.analyticUsers.length; ++i)
{
item.analyticUsers[i]._id = item.analyticUsers[i].geid;
delete item.analyticUsers[i].geid;
}

db.iainproposals.update({_id: item._id}, item);
});

In essence, the sample above would iterate through each document that fits the find parameters and update each subdocument with the change.
This would take a bit longer depending on how many documents you are trying to update, but it will updated your documents and change the field name for each document in the analyticUsers array.

  • Ask Question