Daniel van Dommele
Daniel van Dommele

Reputation: 550

mongo update script: change fields in embedded documents

I am writing a script to update(add and rename) certain fields, which are part of an embedded document within the ones stored in the db collection.

to give an example document:

{
    _id: UUID("025bda29-0d09-4923-8f7f-ea2e825be2c8"),
    name: "test",
    sets: [
       {
           "name": "1",
           "values": [
               {
                   name: "a",
                   value: 5
               }
           ]
       },
       {
           "name": "2",
           "values": [
               {
                   name: "a",
                   value: 3
               }
           ]
       }
   ]
}

This is my script:

function convertGroup (group) {
    for (var i = 0; i < group.sets.length; i++) {
        var set = group.sets[i];
        var oldValuesField = "sets." + i.toString() + ".values";
        var mainValuesField = "sets." + i.toString() + "mainValues";
        var additionalValuesField = "sets." + i.toString() + ".additionalValues";
        db.getCollection('group').updateOne({ 
            "_id" : group._id 
        }, { 
            $set: {
                mainValuesField : set.values,
                additionalValuesField : [ ] 
            },
            $unset: {
                oldValuesField: ""
            }
        });
    }
}
db.getCollection('group').find({'sets.0.mainValues': {$exists: false}}).forEach(convertGroup);

According to the documentation the $rename does not work on arrays, that is why I used set and unset.

what happens when I run this code, is that I get the mainValues field and additionalValues field in the group document, and not in the set documents.

this is what I want it to become:

{
    _id: UUID("025bda29-0d09-4923-8f7f-ea2e825be2c8"),
    name: "test",
    sets: [
       {
           "name": "1",
           "mainValues": [
               {
                   name: "a",
                   value: 5
               }
           ],
           "additionalValues": [ ]
        },
        {
           "name": "2",
           "mainValues": [
               {
                   name: "a",
                   value: 3
               }
           ],
           "additionalValues": [ ]
        }
    ]
}

Can anyone explain to me why this happens and how I can make this work the way I want it to?

Upvotes: 1

Views: 437

Answers (1)

Daniel van Dommele
Daniel van Dommele

Reputation: 550

I managed to fix it by rewriting the script like this:

function convertGroup(group) {

    group.sets.forEach(function (set) {
        if (!set.mainValues) {
            $set : {
                set.mainValues = set.values
            }
       }

        if (!set.additionalValues) {
            $set : {
                set.additionalValues = []
            }
        }
    });
    db.getCollection('group').update({'_id': group._id}, group);
}
db.getCollection('group').find({'sets.mainValues': {$exists: false}}).forEach(convertGroup)

the different is mostly not using the notation 'sets.[index].values' but editing it directly on the json and using 'update' instead of 'updateOne'

Upvotes: 1

Related Questions