More Related Content
Similar to Creating, Updating and Deleting Document in MongoDB
Similar to Creating, Updating and Deleting Document in MongoDB (20)
More from Wildan Maulana (20)
Creating, Updating and Deleting Document in MongoDB
- 1. © 2010, OpenThink Labs. All Rights Reserved
MongoDB
Creating, Updating and Deleting Document
Wildan Maulana
wildan.m@openthinklabs.com
- 2. © 2010, OpenThink Labs. All Rights Reserved
Overview
● Adding new documents to a collection
● Inserting and Saving Documents
– Batch Insert
– Inserts: Internals and Implications
● Removing documents from a collection
● Removing Documents
– Remove Speed
● Updating existing documents
● Document Replacement
● Using Modifiers
● Upserts
● Choosing the correct level of safety versus speed for all of these operations
● The Fastest Write This Side of Mississippi
- 3. © 2010, OpenThink Labs. All Rights Reserved
Inserting and Saving Documents
● Inserts are the basic method for adding data to
MongoDB. To insert a document into a collection,
use the collection’s insert method:
> db.foo.insert({"bar" : "baz"})
- 4. © 2010, OpenThink Labs. All Rights Reserved
Batch Insert
● If you have a situation where you are inserting multiple documents into a
collection, you can make the insert faster by using batch inserts. Batch
inserts allow you to pass an array of documents to the database.
● A batch insert is a single TCP request, meaning that you do not incur the
overhead of doing hundreds of individual requests
● Batch inserts are intended to be used in applications, such as for
inserting a couple hundred sensor data points into an analytics collection
at once.
● They are useful only if you are inserting multiple documents into a single
collection
● Current versions of MongoDB do not accept messages longer than
16MB
- 5. © 2010, OpenThink Labs. All Rights Reserved
Inserts: Internals and Implications
● When you perform an insert, the driver you are
using converts the data structure into BSON,
which it then sends to the database
● The database understands BSON and checks for
an "_id" key and that the document’s size does
not exceed 4MB, but other than that, it doesn’t do
data validation; it just saves the document to the
database as is.
- 6. © 2010, OpenThink Labs. All Rights Reserved
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.
● > db.mailing.list.remove({"opt-out" :
true})
- 7. © 2010, OpenThink Labs. All Rights Reserved
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 i in range(1000000):
collection.insert({"foo": "bar", "baz": i, "z": 10 - i})
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
46.08 s
import time
from pymongo import Connection
db = Connection().foo
collection = db.bar
start = time.time()
db.drop_collection("bar")
total = time.time() - start
print "%d seconds" % total
0.01 s
- 8. © 2010, OpenThink Labs. All Rights Reserved
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 corrupted: the last update will “win.”
- 9. © 2010, OpenThink Labs. All Rights Reserved
Document Replacement
{
"_id" : ObjectId("4b2b9f67a1f631733d917a7a"),
"name" : "joe",
"friends" : 32,
"enemies" : 2
}
{
"_id" : ObjectId("4b2b9f67a1f631733d917a7a"),
"username" : "joe",
"relationships" :
{
"friends" : 32,
"enemies" : 2
}
}
We want to change that
document into the following:
- 10. © 2010, OpenThink Labs. All Rights Reserved
Document Replacement
> var joe = db.users.findOne({"name" : "joe"});
> joe.relationships = {"friends" : joe.friends, "enemies" : joe.enemies};
{
"friends" : 32,
"enemies" : 2
}
> joe.username = joe.name;
"joe"
> delete joe.friends;
true
> delete joe.enemies;
true
> delete joe.name;
true
> db.users.update({"name" : "joe"}, joe);
- 11. © 2010, OpenThink Labs. All Rights Reserved
Common Mistake
> db.people.find()
{"_id" : ObjectId("4b2b9f67a1f631733d917a7b"), "name" : "joe", "age" : 65},
{"_id" : ObjectId("4b2b9f67a1f631733d917a7c"), "name" : "joe", "age" : 20},
{"_id" : ObjectId("4b2b9f67a1f631733d917a7d"), "name" : "joe", "age" : 49},
> joe = db.people.findOne({"name" : "joe", "age" : 20});
{
"_id" : ObjectId("4b2b9f67a1f631733d917a7c"),
"name" : "joe",
"age" : 20
}
> joe.age++;
> db.people.update({"name" : "joe"}, joe);
E11001 duplicate key on update
- 12. © 2010, OpenThink Labs. All Rights Reserved
Using Modifiers
● Usually only certain portions of a document need
to be updated.
● Partial updates can be done extremely efficiently
by using atomic update modifiers
● Update modifiers are special keys that can be
used to specify complex update operations, such
as altering, adding, or removing keys, and even
manipulating arrays and embedded documents.
- 13. © 2010, OpenThink Labs. All Rights Reserved
Using Modifiers
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"url" : "www.example.com",
"pageviews" : 52
}
> db.analytics.update({"url" : "www.example.com"},
... {"$inc" : {"pageviews" : 1}})
use the "$inc"
modifier to increment the value of the "pageviews" key.
> db.analytics.find()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"url" : "www.example.com",
"pageviews" : 53
}
- 14. © 2010, OpenThink Labs. All Rights Reserved
Getting started with
the "$set" modifier
● "$set" sets the value of a key. If the key does not
yet exist, it will be created. This can be handy for
updating schema or adding user-defined keys
- 15. © 2010, OpenThink Labs. All Rights Reserved
> db.users.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"name" : "joe",
"age" : 30,
"sex" : "male",
"location" : "Wisconsin"
}
> db.users.update({"_id" : ObjectId("4b253b067525f35f94b60a31")},
... {"$set" : {"favorite book" : "war and peace"}})
> db.users.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"name" : "joe",
"age" : 30,
"sex" : "male",
"location" : "Wisconsin",
"favorite book" : "war and peace"
}
> db.users.update({"name" : "joe"},
... {"$set" : {"favorite book" : "green eggs and ham"}})
- 16. © 2010, OpenThink Labs. All Rights Reserved
Getting started with
the "$set" modifier
● "$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"]}})
> db.users.update({"name" : "joe"},
... {"$unset" : {"favorite book" : 1}})
remove the key altogether
with "$unset"
- 17. © 2010, OpenThink Labs. All Rights Reserved
Getting started with
the "$set" modifier
● You can also use "$set" to reach in and change
embedded documents:
> 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"
}
}
- 18. © 2010, OpenThink Labs. All Rights Reserved
Incrementing and decrementing
● The "$inc" modifier can be used to change the value for an existing
key or to create a new key if it does not already exist. It is very
useful for updating analytics, karma, votes, or anything else that
has a changeable, numeric value.
> db.games.insert({"game" : "pinball", "user" : "joe"})
> db.games.update({"game" : "pinball", "user" : "joe"},
... {"$inc" : {"score" : 50}})
> db.games.findOne()
{
"_id" : ObjectId("4b2d75476cc613d5ee930164"),
"game" : "pinball",
"name" : "joe",
"score" : 50
}
- 19. © 2010, OpenThink Labs. All Rights Reserved
Incrementing and decrementing
> db.games.update({"game" : "pinball", "user" : "joe"},
... {"$inc" : {"score" : 10000}})
> db.games.find()
{
"_id" : ObjectId("4b2d75476cc613d5ee930164"),
"game" : "pinball",
"name" : "joe",
"score" : 10050
}
"$inc" is similar to "$set", but it is designed for incrementing (and decrementing)
numbers. "$inc" can be used only on values of type integer, long, or double. If it is used
on any other type of value, it will fail.