How to use $inc $set $unset as a Modifiers in MongoDB
------------------------------------------------------------------------------------------------------------
usually certain portion of documents needs to be updated.
You can update specific fields in a document using atomic update modifies.
update modifiers are special keys that can be used to specify complex update operations, such as
1. altering
2.adding
3.removing keys
4. Manipulating arrays.
5. Manipulating arrays on embedded documents
$inc:-
--------
Suppose we were keeping website analytics in a collection and wanted to increment a
counter each time someone visited a page.
We can use update modifiers to do this increment atomically. Each URL and its number of page views is stored in a document
that looks like this:
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"url" : "www.example.com",
"pageviews" : 52
}
Every time someone visits a page, we can find the page by its URL and use the "$inc"
modifier to increment the value of the "pageviews" key:
> db.analytics.update({"url" : "www.example.com"},
... {"$inc" : {"pageviews" : 1}})
Now, if we do a find, we see that "pageviews" has increased by one:
> db.analytics.find()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"url" : "www.example.com",
"pageviews" : 53
}
When using modifiers, the value of "_id" cannot be changed. (Note that "_id" can be
changed by using whole-document replacement.) Values for any other key, including
other uniquely indexed keys, can be modified.
Getting started with the “$set” modifier
"$set" sets the value of a field. If the field does not yet exist, it will be created. This can
be handy for updating schema or adding user-defined keys. For example, suppose you
have a simple user profile stored as a document that looks something like the following:
> db.users.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"name" : "joe",
"age" : 30,
"sex" : "male",
"location" : "Wisconsin"
}
This is a pretty bare-bones user profile. If the user wanted to store his favorite book in
his profile, he could add it using "$set":
> db.users.update({"_id" : ObjectId("4b253b067525f35f94b60a31")},
... {"$set" : {"favorite book" : "War and Peace"}})
Now the document will have a “favorite book” key:
> db.users.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"name" : "joe",
"age" : 30,
"sex" : "male",
"location" : "Wisconsin",
"favorite book" : "War and Peace"
}
If the user decides that he actually enjoys a different book, "$set" can be used again to
change the value:
> db.users.update({"name" : "joe"},
... {"$set" : {"favorite book" : "Green Eggs and Ham"}})
"$set" can even change the type of the key it modifies. For instance, if our fickle user
decides that he actually likes quite a few books, he can change the value of the “favorite
book” key into an array:
> db.users.update({"name" : "joe"},
... {"$set" : {"favorite book" :
... ["Cat's Cradle", "Foundation Trilogy", "Ender's Game"]}})
$unset
If the user realizes that he actually doesn’t like reading, he can remove the key altogether
with "$unset":
> db.users.update({"name" : "joe"},
... {"$unset" : {"favorite book" : 1}})
Now the document will be the same as it was at the beginning of this example.
We can also use $set modifiers to reach in and change embedded documents:
You can also use "$set" to
> db.blog.posts.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"title" : "A Blog Post",
"content" : "...",
"author" : {
"name" : "joe",
"email" : "joe@example.com"
}
}
> db.blog.posts.update({"author.name" : "joe"},
... {"$set" : {"author.name" : "joe schmoe"}})
> db.blog.posts.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"title" : "A Blog Post",
"content" : "...",
"author" :
{
"name" : "joe schmoe",
"email" : "joe@example.com"
}
}
Thanks for reading..
------------------------------------------------------------------------------------------------------------
usually certain portion of documents needs to be updated.
You can update specific fields in a document using atomic update modifies.
update modifiers are special keys that can be used to specify complex update operations, such as
1. altering
2.adding
3.removing keys
4. Manipulating arrays.
5. Manipulating arrays on embedded documents
$inc:-
--------
Suppose we were keeping website analytics in a collection and wanted to increment a
counter each time someone visited a page.
We can use update modifiers to do this increment atomically. Each URL and its number of page views is stored in a document
that looks like this:
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"url" : "www.example.com",
"pageviews" : 52
}
Every time someone visits a page, we can find the page by its URL and use the "$inc"
modifier to increment the value of the "pageviews" key:
> db.analytics.update({"url" : "www.example.com"},
... {"$inc" : {"pageviews" : 1}})
Now, if we do a find, we see that "pageviews" has increased by one:
> db.analytics.find()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"url" : "www.example.com",
"pageviews" : 53
}
When using modifiers, the value of "_id" cannot be changed. (Note that "_id" can be
changed by using whole-document replacement.) Values for any other key, including
other uniquely indexed keys, can be modified.
Getting started with the “$set” modifier
"$set" sets the value of a field. If the field does not yet exist, it will be created. This can
be handy for updating schema or adding user-defined keys. For example, suppose you
have a simple user profile stored as a document that looks something like the following:
> db.users.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"name" : "joe",
"age" : 30,
"sex" : "male",
"location" : "Wisconsin"
}
This is a pretty bare-bones user profile. If the user wanted to store his favorite book in
his profile, he could add it using "$set":
> db.users.update({"_id" : ObjectId("4b253b067525f35f94b60a31")},
... {"$set" : {"favorite book" : "War and Peace"}})
Now the document will have a “favorite book” key:
> db.users.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"name" : "joe",
"age" : 30,
"sex" : "male",
"location" : "Wisconsin",
"favorite book" : "War and Peace"
}
If the user decides that he actually enjoys a different book, "$set" can be used again to
change the value:
> db.users.update({"name" : "joe"},
... {"$set" : {"favorite book" : "Green Eggs and Ham"}})
"$set" can even change the type of the key it modifies. For instance, if our fickle user
decides that he actually likes quite a few books, he can change the value of the “favorite
book” key into an array:
> db.users.update({"name" : "joe"},
... {"$set" : {"favorite book" :
... ["Cat's Cradle", "Foundation Trilogy", "Ender's Game"]}})
$unset
If the user realizes that he actually doesn’t like reading, he can remove the key altogether
with "$unset":
> db.users.update({"name" : "joe"},
... {"$unset" : {"favorite book" : 1}})
Now the document will be the same as it was at the beginning of this example.
We can also use $set modifiers to reach in and change embedded documents:
You can also use "$set" to
> db.blog.posts.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"title" : "A Blog Post",
"content" : "...",
"author" : {
"name" : "joe",
"email" : "joe@example.com"
}
}
> db.blog.posts.update({"author.name" : "joe"},
... {"$set" : {"author.name" : "joe schmoe"}})
> db.blog.posts.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"title" : "A Blog Post",
"content" : "...",
"author" :
{
"name" : "joe schmoe",
"email" : "joe@example.com"
}
}
Thanks for reading..