Database Reference
In-Depth Information
Deleting data is much simpler. You simply use the remove method. This method takes
an optional query selector which will remove only those documents matching the
selector. If no selector is provided, all documents in the collection will be removed.
Here, you're removing all user documents where the age attribute is greater than or
equal to 40:
@users.remove({"age" => {"$gte" => 40}})
With no arguments, the remove method deletes all remaining documents:
@users.remove
You may recall from the previous chapter that remove doesn't actually drop the collec-
tion. To drop a collection and all its indexes, use the drop_collection method:
connection = Mongo::Connection.new
db = connection['tutorial']
db.drop_collection('users')
3.1.5
Database commands
You saw in the last chapter the centrality of database commands. There, we looked at
the two stats commands. Here, we'll look at how you can run commands from the
driver using the listDatabases command as an example. This is one of a number of
commands that must be run on the admin database, which is treated specially when
authentication is enabled. For details on the authentication and the admin database,
see chapter 10.
First, you instantiate a Ruby database object referencing the admin database. You
then pass the command's query specification to the command method:
@admin_db = @con['admin']
@admin_db.command({"listDatabases" => 1})
The response is a Ruby hash listing all the existing databases and their sizes on disk:
{
"databases" => [
{
"name" => "tutorial",
"sizeOnDisk" => 218103808,
"empty" => false
},
{
"name" => "admin",
"sizeOnDisk" => 1,
"empty" => true
},
{
"name" => "local",
"sizeOnDisk" => 1,
"empty" => true
}
],
Search WWH ::




Custom Search