0

I have a document with child elements of items like so

"bar" : "547244fe10f0edd3128b4567",
    "items" : [ 
        {
            "1" : {
                "message" : "",
                "display" : "true",
                "type" : "text"
            }
        }, 
        {
            "2" : {
                "id" : "234234",
                "type" : "image",
                "message" : "foo",
                "display" : "true",
                "created_at" : NumberLong(1416432114)
            }
        }, 
        {
            "3" : {
                "message" : "",
                "display" : "true",
                "type" : "text"
            }
        }, 

and I'm trying to update one of the childrens value

$foo['items']['1']['message'] = 'hello';
$story = InfoDB::where('_id', $id)->update($foo);

So that

        "1" : {
            "message" : "",
            "display" : "true",
            "type" : "text"
        }

Becomes

        "1" : {
            "message" : "hello",
            "display" : "true",
            "type" : "text"
        }

But when I run the update command it deletes all the children in the document.

Do I have to update the entire document? or is there another function?

I'm using https://github.com/jenssegers/laravel-mongodb

1 Answer 1

1

Since every one of you 'items' is an item of its own an has therefore an own array index, you have to make a little adjustment to your code. So just try to replace this code

$foo['items']['1']['message'] = 'hello';

with this one

$foo['items'][0]['1']['message'] = 'hello';

This will call the first item (0) and then the specific key within the item ("1").

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.