Anna Jeanine
Anna Jeanine

Reputation: 4125

Remove embedded document from MongoDB PHP

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?

Upvotes: 0

Views: 341

Answers (1)

simon
simon

Reputation: 2946

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' => '')));

Upvotes: 2

Related Questions