Database Reference
In-Depth Information
CouchDB also lets you write your queries as map and reduce functions. If that sounds
like a lot of effort, then you're in good company; CouchDB has a somewhat severe
learning curve. In fairness to CouchDB, an experienced programmer can probably pick it
up quite quickly; for most people, however, the learning curve is probably steep enough
that they won't bother with the tool.
Fortunately for us mere mortals, MongoDB is much easier to use. We'll cover how
to use MongoDB in more detail throughout the topic, but here's the short version: in
MongoDB, you simply provide the parts of the document you want to match against, and
MongoDB does the rest. MongoDB can do much more, however. For example, you won't
find MongoDB lacking if you want to use map or reduce functions. At the same time, you can
ease into using MongoDB; you don't have to know all of the tool's advanced features up front.
Indexing Your Documents
MongoDB includes extensive support for indexing your documents, a feature that really
comes in handy when you're dealing with tens of thousands of documents. Without an
index, MongoDB will have to look at each individual document in turn to see whether it
is something that you want to see. This is like asking a librarian for a particular book and
watching as he works his way around the library looking at each and every topic. With an
indexing system (libraries tend to use the Dewey Decimal system), he can find the area
where the topic you are looking for lives and very quickly determine if it is there.
Unlike a library topic, all documents in MongoDB are automatically indexed on the
_id key. This key is considered a special case because you cannot delete it; the index is
what ensures that each value is unique. One of the benefits of this key is that you can be
assured that each document is uniquely identifiable, something that isn't guaranteed by
an RDBMS.
When you create your own indexes, you can decide whether you want them to
enforce uniqueness. If you do decide to create a unique index, you can tell MongoDB
to drop all the duplicates. This may or may not be what you want, so you should think
carefully before using this option because you might accidentally delete half your data.
By default, an error will be returned if you try to create a unique index on a key that has
duplicate values.
There are many occasions where you will want to create an index that allows
duplicates. For example, if your application searches by lastname, it makes sense to build
an index on the lastname key. Of course, you cannot guarantee that each lastname will be
unique; and in any database of a reasonable size, duplicates are practically guaranteed.
MongoDB's indexing abilities don't end there, however. MongoDB can also create
indexes on embedded documents. For example, if you store numerous addresses in the
address key, you can create an index on the ZIP or postal code. This means that you can
easily pull back a document based on any postal codeā€”and do so very quickly.
MongoDB takes this a step further by allowing composite indexes . In a composite
index, two or more keys are used to build a given index. For example, you might build
an index that combines both the lastname and firstname tags. A search for a full name
would be very quick because MongoDB can quickly isolate the lastname and then, just as
quickly, isolate the firstname.
We will look at indexing in more depth in Chapter 10, but suffice it to say that
MongoDB has you covered as far as indexing is concerned.
 
Search WWH ::




Custom Search