0

I have a collection in mongoDb. it is similar to the following.

array(
    '_id' => new MongoId("50b35d1217ce10ac1000000f")
    'Education' => 
      array (
        'content' => 
        array (
          '0' => 
          array (
            'Organization' => 'SUST',
            'Degree' => 'BSC',
            'Department' => '',
            'Location' => 'Dhaka',
            'Session' => '2 Years',
          ),
          '1' => 
          array (
            'Organization' => 'DU',
            'Degree' => 'BSC',
            'Department' => '',
            'Location' => 'Dhaka',
            'Session' => '2 Years',
          )    
        ),
        'sharing' => 'public',
      ),
)

I want to delete Education.content.1 from the collection. So i used the

update(array('_id' => new MongoId('50b35d1217ce10ac1000000f')), array('$unset' => array('Education.content.1' => 1)));

As a result Education.content.1 becomes null. But I want Education.content.1 to be deleted not to be null.

Please help me if any one knows the solution. Thanks in advance.

1 Answer 1

1

Use $pull after $unset:

update(array('_id' => new MongoId('50b35d1217ce10ac1000000f')), 
array('$unset' => array('Education.content.1' => 1)));
update(array('_id' => new MongoId('50b35d1217ce10ac1000000f')), 
array('$pull' => array('Education.content' => null)));
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.