Databases Reference
In-Depth Information
Now the document has a "comments" key. If we call find again, we can see the new key:
> db.blog.find()
{
"_id" : ObjectId("4b23c3ca7525f35f94b60a2d"),
"title" : "My Blog Post",
"content" : "Here's my blog post.",
"date" : "Sat Dec 12 2009 11:23:21 GMT-0500 (EST)"
"comments" : [ ]
}
Delete
remove deletes documents permanently from the database. Called with no parameters,
it removes all documents from a collection. It can also take a document specifying
criteria for removal. For example, this would remove the post we just created:
> db.blog.remove({title : "My Blog Post"})
Now the collection will be empty again.
Tips for Using the Shell
Because mongo is simply a JavaScript shell, you can get a great deal of help for it by
simply looking up JavaScript documentation online. The shell also includes built-in
help that can be accessed by typing help :
> help
HELP
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show recent system.profile entries w. time >= 1ms
use <db name> set current database to <db name>
db.help() help on DB methods
db.foo.help() help on collection methods
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated
Help for database-level commands is provided by db.help(); , and help at the collec-
tions can be accessed with db.foo.help(); .
A good way of figuring out what a function is doing is to type it without the parentheses.
This will print the JavaScript source code for the function. For example, if we are curious
about how the update function works or cannot remember the order of parameters, we
can do the following:
> db.foo.update
function (query, obj, upsert, multi) {
assert(query, "need a query");
assert(obj, "need an object");
this._validateObject(obj);
this._mongo.update(this._fullName, query, obj,
 
Search WWH ::




Custom Search