0

I have a collection like this in this collection I need to pull or remove the details from array where login date = 2015-12-23.Rest of the array should be same

Array
(
    [_id] => 1
    [user_id] => 2
    [work_details] => Array
        (
           [0] => Array
                (
                    [login_date] => 2015-12-23
                    [work_history] => Array
                        (
                            [0] => Array
                                (
                                    [details] => gsdgsdgd
                                    [datetime] => 2015-12-23 12:54:12
                                )

                        )

                )
           [1] => Array
                (
                    [login_date] => 2015-12-24
                    [work_history] => Array
                        (
                            [0] => Array
                                (
                                    [details] => ffgdf dfgdfh
                                    [datetime] => 2015-12-24 12:54:12
                                )

                        )

                )


        )
)

I have tried like this

$this->mongo_db->where('login_date', "2015-12-23")->unset_field('login_date.$')->update('masterCollection');
$this->mongo_db->pull('login_date', NULL)->update('masterCollection');

but its not working

After removing the record I need the array like this

Array
(
    [_id] => 1
    [user_id] => 2
    [work_details] => Array
        (
            [0] => Array
                (
                    [login_date] => 2015-12-24
                    [work_history] => Array
                        (
                            [0] => Array
                                (
                                    [details] => ffgdf dfgdfh
                                    [datetime] => 2015-12-24 12:54:12
                                )

                        )

                )


        )
)

Is there any solution for this.

Thank you

1 Answer 1

1

Here is the query that I used to remove the data:

db.testso.update({_id: 2},{ $pull: { work_details: {login_date:"2015-12-25"}}});

It successfully removed the array with the login_date: "2015-12-25".

Here is the sample data that I used:

{ "_id" : 1, "user_id" : 2, "work_details" : [ { "login_date" : "2015-12-23", "work_history" : [ { "details" : "abcd", "day" : "ancxc" } ] } ] }
{ "_id" : 2, "user_id" : 3, "work_details" : [ { "login_date" : "2015-12-24", "work_history" : [ { "details" : "abcd1", "day" : "ancxc1" } ] }, { "login_date" : "2015-12-25", "work_history" : [ { "details" : "abcd2", "day" : "ancxc2" } ] } ] }
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.