Databases Reference
In-Depth Information
$min : document
Start criteria for querying.
$max : document
End criteria for querying.
$hint : document
Tell the server which index to use for the query.
$explain : boolean
Get an explanation of how the query will be executed (indexes used, number of
results, how long it takes, etc.), instead of actually doing the query.
$snapshot : boolean
Ensure that the query's results will be a consistent snapshot from the point in time
when the query was executed. See the next section for details.
Getting Consistent Results
A fairly common way of processing data is to pull it out of MongoDB, change it in some
way, and then save it again:
cursor = db.foo.find();
while (cursor.hasNext()) {
var doc = cursor.next();
doc = process(doc);
db.foo.save(doc);
}
This is fine for a small number of results, but it breaks down for large numbers of
documents. To see why, imagine how the documents are actually being stored. You
can picture a collection as a list of documents that looks something like Figure 4-1 .
Snowflakes represent documents, because every document is beautiful and unique.
Figure 4-1. A collection being queried
Now, when we do a find , it starts returning results from the beginning of the collection
and moves right. Your program grabs the first 100 documents and processes them.
When you save them back to the database, if a document does not have the padding
available to grow to its new size, like in Figure 4-2 , it needs to be relocated. Usually, a
document will be relocated to the end of a collection ( Figure 4-3 ).
 
Search WWH ::




Custom Search