laravelmongodbjenssegers-mongodb

Laravel - Jenssegers MongoDB nested array update not working


can someone please point out what wrong with my code? I am trying to update a nested array in MongoDB via Laravel Jenssegers. Here is my code

 $update_status = Journal::where('_id', "5cd10b325586e9122761f675" )
    ->update(
        [], 
        [ '$set' =>
            [
                "workflow.$[i].stages.$[j].stage_code" => "edit",
                "workflow.$[i].stages.$[j].stage_name" => "Editing"
            ]
        ],
        [ 'arrayFilters' => [
                [ "i.basic_details.wfCode" => 'wf1' ],
                [ "j.stage_id" => "wf1_2" ]
            ]
        ]
    );

and my Journal collection is:

{
    "_id" : ObjectId("5cd10b325586e9122761f675"),
    "workflow" : [ 
        {
            "basic_details" : {
                "wfCode" : "wf1"
            },
            "stages" : [ 
                {
                    "stage_id" : "wf1_1",
                    "stage_code" : "submission",
                    "stage_name" : "Submission",
                    "button_label" : "submit"
                }, 
                {
                    "stage_id" : "wf1_2",
                    "stage_code" : "s2",
                    "stage_name" : "S2",
                    "button_label" : "label2"
                }
            ]
        }
    ]
}

After execution, I am getting $update_status as 1, but while looking into the collection, it remains unchanged.


Solution

  • Please check this query, i think this should work...

    $articleId = new ObjectID("5cd10b325586e9122761f675");
    $result = Journal::raw(function ($collection) use ($articleId) {
    return $collection->updateOne(
        array ('_id' => $articleId), 
        array ('$set' => 
            array(
                'jnl_workflow.$[i].wf_stages.$[j].wf_stage_code' => "edit",
                'jnl_workflow.$[i].wf_stages.$[j].wf_stage_name' => "Editing" 
            )
        ), 
        array( 'arrayFilters' => [ array ('i.wf_basic_details.wfCode' => 'wf1'), array('j.wf_stage_id' => "wf1_2" ) ] ) 
    )
    

    });