Database Reference
In-Depth Information
> db.users.find()
{ _id: ObjectId("4ba667b0a90578631c9caea0"), name: "Kyle" }
The find method returns the inserted document, with the an object ID added. All
documents require a primary key stored in the _id field. You're allowed to enter a cus-
tom _id as long as you can guarantee its uniqueness. But if you omit the _id alto-
gether, then a MongoDB object ID will be inserted automatically.
In addition to allowing you to insert and query for data, the shell permits you to
run administrative commands. Some examples include viewing the current database
operation, checking the status of replication to a secondary node, and configuring a
collection for sharding. As you'll see, the MongoDB shell is indeed a powerful tool
that's worth getting to know well.
All that said, the bulk of your work with MongoDB will be done through an appli-
cation written in a given programming language; to see how that's done, we must say a
few things about MongoDB's language drivers.
1.3.3
Database drivers
If the notion of a database driver conjures up nightmares of low-level device hacking,
don't fret. The MongoDB drivers are easy to use. Every effort has been made to pro-
vide an API that matches the idioms of the given language while also maintaining rela-
tively uniform interfaces across languages. For instance, all of the drivers implement
similar methods for saving a document to a collection, but the representation of the
document itself will usually be whatever is most natural to each language. In Ruby,
that means using a Ruby hash. In Python, a dictionary is appropriate. And in Java,
which lacks any analogous language primitive, you represent documents using a spe-
cial document builder class that implements LinkedHashMap .
Because the drivers provide a rich, language-centric interface to the database, little
abstraction beyond the driver itself is required to build an application. This contrasts
notably with the application design for an RDBMS , where a library is almost certainly
necessary to mediate between the relational data model of the database and the
object-oriented model of most modern programming languages. Still, even if the heft
of an object-relational mapper isn't required, many developers like using a thin wrap-
per over the drivers to handle associations, validations, and type checking. 8
At the time of this writing, 10gen officially supports drivers for C, C++, C#, Erlang,
Haskell, Java, Perl, PHP , Python, Scala, and Ruby—and the list is always growing. If you
need support for another language, there's probably a community-supported driver
for it. If no community-supported driver exists for your language, specifications for
building a new driver are documented at mongodb.org. Since all of the officially sup-
ported drivers are used heavily in production and provided under the Apache license,
plenty of good examples are freely available for would-be driver authors.
Beginning in chapter 3, I describe how the drivers work and how to use them to
write programs.
8
A few popular wrappers at the time of this writing include Morphia for Java, Doctrine for PHP, and Mongo-
Mapper for Ruby.
Search WWH ::




Custom Search