2

Hi I am new to mongodb.. My problem is to append array in mongodb data

My array

{
    "_id" : ObjectId("5864f61111115810fc011111"),
    "estimate" : {
        "estimate_id" : 1122332,
        "source_data" : {
            "1" : {
                "test":"test"
            }
        }
    },
    "updated_at" : ISODate("1970-01-15T10:47:01.399Z"),
    "created_at" : ISODate("1970-01-15T10:41:56.623Z")
}

I want to add array in source_data like bellow

{
    "_id" : ObjectId("5864f61111115810fc011111"),
    "estimate" : {
        "estimate_id" : 1122332,
        "source_data" : {
            "1" : {
                "name":"nikhil"
            },
            "2" : {
                "name":"nikhil"
            }
        }
    },
    "updated_at" : ISODate("1970-01-15T10:47:01.399Z"),
    "created_at" : ISODate("1970-01-15T10:41:56.623Z")
}

I have tried below code but not working

$data = array("2"=>array("name":"nikhil"));
$sourcing = Sourcing::find('5864f61111115810fc011111');//return mongo data
$sourcing->put('estimate.source_data.2',$data );

1 Answer 1

2

To append one or more values to an array, the following which uses the push() method should work for you:

$data = array('name' => 'nikhil');
Sourcing::find('5864f61111115810fc011111')->push('estimate.source_data', $data);
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.