Databases Reference
In-Depth Information
slowdown, as arrays get longer, it takes MongoDB a longer amount of time to traverse
the whole array, slowing down each array modification.
A simple program in Python can demonstrate the speed difference. This program inserts
a single key and increments its value 100,000 times.
from pymongo import Connection
import time
db = Connection().performance_test
db.drop_collection("updates")
collection = db.updates
collection.insert({"x": 1})
# make sure the insert is complete before we start timing
collection.find_one()
start = time.time()
for i in range(100000):
collection.update({}, {"$inc" : {"x" : 1}})
# make sure the updates are complete before we stop timing
collection.find_one()
print time.time() - start
On a MacBook Air this took 7.33 seconds. That's more than 13,000 updates per second
(which is pretty good for a fairly anemic machine). Now, let's try it with a document
with a single array key, pushing new values onto that array 100,000 times:
for i in range(100000):
collection.update({}, {'$push' : {'x' : 1}})
This program took 67.58 seconds to run, which is less than 1,500 updates per second.
Using "$push" and other array modifiers is encouraged and often necessary, but it is
good to keep in mind the trade-offs of such updates. If "$push" becomes a bottleneck,
it may be worth pulling an embedded array out into a separate collection.
Upserts
An upsert is a special type of update. If no document is found that matches the update
criteria, a new document will be created by combining the criteria and update docu-
ments. If a matching document is found, it will be updated normally. Upserts can be
very handy because they eliminate the need to “seed” your collection: you can have the
same code create and update documents.
Let's go back to our example recording the number of views for each page of a website.
Without an upsert, we might try to find the URL and increment the number of views
or create a new document if the URL doesn't exist. If we were to write this out as a
 
Search WWH ::




Custom Search