1

I want to use PHP to remove documents from a MongoDB collection, but it's a capped collection (and it's stuck that way until I can get more memory), which means I can't remove documents. I have to do the next best thing. How can I unset all the fields in a document?

$collection->update(array('name' => 'Amy'), array('$unset' => array(WHAT GOES HERE?)));

Of course, I could list all the fields I want to unset, but then I'd have to change the code any time a new field was added. Can I unset all fields without knowing the names of all the fields?

asked Aug 15, 2014 at 14:51

1 Answer 1

2

To remove all fields (aside from _id), you can just update with an empty document:

<?php
 $collection->update(array('name' => 'Amy'), array());
?>

That will update the document leaving only the _id field.

Note that this won't free up any of the preallocated space for your capped collection.

answered Aug 15, 2014 at 23:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.