Database Reference
In-Depth Information
which includes the name of the database it belongs to. Thus, the products collection
is technically referred to as garden.products when referenced in a message to or
from the core server. This fully qualified collection name can't be longer than 128
characters.
It's sometimes useful to include the . character in collection names to provide a
kind of virtual namespacing. For instance, you can imagine a series of collections with
titles like the following:
products.categories
products.images
products.reviews
Keep in mind that this is just an organizational principle; the database treats collec-
tions named with a . just like any other collection.
Because I've already spoken about removing documents from collections and
dropping collections completely, you only need to note that collections can also be
renamed. As an example, you can rename the products collection with the shell's
renameCollection method:
db.products.renameCollection("store_products")
C APPED COLLECTIONS
In addition to the standard collections you've created so far, it's also possible to create
what's known as a capped collection . Capped collections were originally designed for
high-performance logging scenarios. They're distinguished from standard collections
by their fixed size. This means that once a capped collection reaches its maximum
size, subsequent inserts will overwrite the least-recently-inserted documents in the col-
lection. This design prevents users from having to prune the collection manually
when only recent data may be of value.
To understand how you might use a capped collection, imagine you want to keep
track of users' actions on your site. Such actions might include viewing a product, add-
ing to the cart, checking out, and purchasing. You can write a script to simulate log-
ging these user actions to a capped collection. In the process, you'll see some of these
collections' interesting properties. Here's a simple demonstration.
Listing 4.6
Simulating the logging of user actions to a capped collection
require 'rubygems'
require 'mongo'
VIEW_PRODUCT = 0
ADD_TO_CART = 1
CHECKOUT
= 2
PURCHASE
= 3
@con = Mongo::Connection.new
@db = @con['garden']
@db.drop_collection("user.actions")
@db.create_collection("user.actions", :capped => true, :size => 1024)
 
Search WWH ::




Custom Search