Databases Reference
In-Depth Information
Removing Documents
Now that there's data in our database, let's delete it.
> db.users.remove()
This will remove all of the documents in the
users
collection. This doesn't actually
remove the collection, and any indexes created on it will still exist.
The
remove
function optionally takes a query document as a parameter. When it's given,
only documents that match the criteria will be removed. Suppose, for instance, that we
want to remove everyone from the
mailing.list
collection where the value for
"opt-
out"
is
true
:
> db.mailing.list.remove({"opt-out" : true})
Once data has been removed, it is gone forever. There is no way to undo the remove
or recover deleted documents.
Remove Speed
Removing documents is usually a fairly quick operation, but if you want to clear an
entire collection, it is faster to
drop
it (and then re-create any indexes).
For example, in Python, suppose we insert a million dummy elements with the
following:
for i in range(1000000):
collection.insert({"foo": "bar", "baz": i, "z": 10 - i})
Now we'll try to remove all of the documents we just inserted, measuring the time it
takes. First, here's a simple
remove
:
import time
from pymongo import Connection
db = Connection().foo
collection = db.bar
start = time.time()
collection.remove()
collection.find_one()
total = time.time() - start
print "%d seconds" % total
On a MacBook Air, this script prints “46.08 seconds.”
If the
remove
and
find_one
are replaced by
db.drop_collection("bar")
, the time drops
to .01 seconds! This is obviously a vast improvement, but it comes at the expense of