3

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.

2
  • 1
    which version of mongodb are you using? Commented Sep 24, 2019 at 10:25
  • 1
    since nested array filters support only 3.6 above, I am using MongoDB v 4 @AnoopSankar Commented Sep 24, 2019 at 10:27

1 Answer 1

3

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" ) ] ) 
)

});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.