Database Reference
In-Depth Information
db.foo.getDB(
db.foo.getIndexes(
db.foo.getShardVersion(
db.foo.getFullName( db.foo.getIndices(
db.foo.getIndexKeys( db.foo.getMongo(
If you're more ambitious, and comfortable with JavaScript, the shell makes it easy to
examine the implementation of any given method. For instance, suppose you'd like to
know exactly how the save() method works. Sure, you could go trolling through the
MongoDB source code, but there's an easier way. Simply enter the method name with-
out the executing parentheses. Here's how you'd normally execute save() :
> db.numbers.save({num: 123123123});
And this is how you can check the implementation:
> db.numbers.save
function (obj) {
if (obj == null || typeof obj == "undefined") {
throw "can't save a null";
}
if (typeof obj._id == "undefined") {
obj._id = new ObjectId;
return this.insert(obj);
} else {
return this.update({_id:obj._id}, obj, true);
}
}
Read the function definition closely, and you'll see that save() is merely a wrapper for
insert() and update() . If the object you're trying to save doesn't have an _id field,
then the field is added, and insert() is invoked; otherwise, an update is performed.
This trick for examining the shell's methods comes in handy; keep this technique
at hand as you continue exploring the MongoDB shell.
2.5
Summary
You've now seen the document data model in practice, and we've demonstrated a vari-
ety of common MongoDB operations on that data model. You've learned how to cre-
ate indexes, and seen a real-life example of index-based performance improvements
through the use of explain() . In addition, you should be able to extract information
about the collections and databases on your system, you now know all about the clever
$cmd collection, and if you ever need help, you've picked up a few tricks for finding
your way around.
You can learn a lot by working in the MongoDB shell, but there's no substitute for
the experience of building a real application. That's why, in the next chapter, we're
going from a carefree data playground to a real-world data workshop. You'll see how
the drivers work, and then, using the Ruby driver, you'll build a simple application,
hitting MongoDB with some real live data.
Search WWH ::




Custom Search