Database Reference
In-Depth Information
If the command executes successfully, an OK will be returned. If it fails, however
(if the collection doesn't exist, for example), then the following message is returned:
{ "errmsg" : "assertion: source namespace does not exist", "ok" : 0 }
The renameCollection command doesn't take many parameters (unlike some
commands you've seen so far); however, it can be quite useful in the right circumstances.
Removing Data
So far we've explored how to add, search for, and modify data. Next, we'll examine how to
remove documents, entire collections, and the databases themselves.
Previously, you learned how to remove data from a specific document (using
the $pop command, for instance). In this section, you will learn how to remove full
documents and collections. Just as the insert() function is used for inserting and
update() is used for modifying a document, remove() is used to remove a document.
To remove a single document from your collection, you need to specify the criteria
you'll use to find the document. A good approach is to perform a find() first; this ensures
that the criteria used are specific to your document. Once you are sure of the criterion,
you can invoke the remove() function using that criterion as a parameter:
> db.newname.remove( { "Title" : "Different Title" } )
This statement removes the topic added previously or any other item in your
collection that has the same title. The fact this statement removes all topics by that title is
one reason why it's best to specify the item's _id value—it's always unique.
Or you can use the following snippet to remove all documents from the newname
library (remember, we renamed the media collection this previously):
> db.newname.remove({})
When removing a document, you need to remember that any reference to
that document will remain within the database. For this reason, be sure you manually
delete or update those references as well; otherwise, these references will return null
when evaluated. referencing will be discussed in the next section.
Warning
If you want to remove an entire collection, you can use the drop() function. The
following snippet removes the entire newname collection, including all of its documents:
> db.newname.drop()
true
 
 
Search WWH ::




Custom Search