mongoDB UPDATE - Workaround in Positional $ Operator issue

224 views Asked by At

I've been searching for quite some time about updating a nested array in mongoDB. So far, I know that there's an issue on positional $ operator on deep array, see this : https://jira.mongodb.org/browse/SERVER-831

But is there anyone in here who could help me create a workaround for this? This is my sample schema :

{
    folder_data : [
        {
            "folderName" : "First",
            "otherFolder" : []
        },
        {
            "folderName" : "Second",
            "otherFolder" : [
                {
                    "folderName" : "SecondA",
                    "otherFolder" : [
                        {
                            "folderName" : "SecondB",
                            "otherFolder" : [

                                // put my new data here                             

                            ]
                        },
                        {
                            "folderName" : "SecondBextra",
                            "otherFolder" : []
                        }
                    ]
                },
                {
                    "folderName" : "SecondAextra",
                    "otherFolder" : []
                },
            ]
        }
    ]   
}

and this is so far i've done :

Working (for first array) :

$collection->update(
            array('folder_data.folderName' => 'Second'), 
            array('$set' => array('folder_data.$.otherFolder' => array('test' => 'sample')))
        );

Not working :

$collection->update(
            array(
                'folder_data.folderName' => 'Second',
                'folder_data.otherFolder.folderName' => 'SecondA',
                'folder_data.otherFolder.otherFolder.folderName' => 'SecondB'
            ), 
            array('$set' => array('folder_data.$.otherFolder.$.otherFolder.$.otherFolder' => array('test' => 'sample'))));
0

There are 0 answers