Databases Reference
In-Depth Information
granularity: we cannot specify any criteria. The whole collection is dropped, and all of
its indexes are deleted.
Updating Documents
Once a document is stored in the database, it can be changed using the update method.
update takes two parameters: a query document, which locates documents to update,
and a modifier document, which describes the changes to make to the documents
found.
Updates are atomic: if two updates happen at the same time, whichever one reaches
the server first will be applied, and then the next one will be applied. Thus, conflicting
updates can safely be sent in rapid-fire succession without any documents being cor-
rupted: the last update will “win.”
Document Replacement
The simplest type of update fully replaces a matching document with a new one. This
can be useful to do a dramatic schema migration. For example, suppose we are making
major changes to a user document, which looks like the following:
{
"_id" : ObjectId("4b2b9f67a1f631733d917a7a"),
"name" : "joe",
"friends" : 32,
"enemies" : 2
}
We want to change that document into the following:
{
"_id" : ObjectId("4b2b9f67a1f631733d917a7a"),
"username" : "joe",
"relationships" :
{
"friends" : 32,
"enemies" : 2
}
}
We can make this change by replacing the document using an update :
> var joe = db.users.findOne({"name" : "joe"});
> joe.relationships = {"friends" : joe.friends, "enemies" : joe.enemies};
{
"friends" : 32,
"enemies" : 2
}
 
Search WWH ::




Custom Search