0

I am trying to build a query that deletes an embedded document from a MongoDB document in PHP. What I have now is:

$collection->update(array("_id" => new MongoId($id)),
        array('$unset' => 'BUSCO.short_summary_data'));

I have also tried:

$collection->remove(array("_id" => new MongoId($id)),
        array('$unset' => 'BUSCO.short_summary_data'));

No error is thrown, but the embedded document still exists! Could someone help me out?

1 Answer 1

2

Your current statement written in JSON looks like this:

{ $unset: 'BUSCO.short_summary_data' }

But according to the documentation:

The $unset operator deletes a particular field. Consider the following syntax:

{ $unset: { <field1>: "", ... } }

The specified value in the $unset expression (i.e. "") does not impact the operation.

So $unset expects an array with key-value pairs. Try:

$collection->update(array("_id" => new MongoId($id)),
        array('$unset' => array('BUSCO.short_summary_data' => '')));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is what I was looking for :-)

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.