Jump to content

Reference of Frequently Used MongoDB Commands

0
  chco's Photo
Posted Sep 13 2010 10:30 AM

The following excerpt is from MongoDB: The Definitive Guide. Below we have a reference of the most common commands used when administering MongoDB.
At the time of this writing, MongoDB supports more than 75 different commands, and more commands are being added all the time. There are two ways to get an up-to-date list of all of the commands supported by a MongoDB server:

  • Run db.listCommands() from the shell, or run the equivalent listCommands command from any other driver.

  • Browse to the http://localhost:28017/_commands URL on the MongoDB admin interface.


The following list contains some of the most frequently used MongoDB commands along with example documents showing how each command should be represented:


buildInfo

{"buildInfo" : 1}


Admin-only command that returns information about the MongoDB server’s version number and host operating system.



collStats

{"collStats" : collection}

Gives some stats about a given collection, including its data size, the amount of storage space allocated for it, and the size of its indexes.



distinct

{"distinct" : collection, "key": key, "query": query}

Gets a list of distinct values for key in documents matching query, across a given collection.



drop

{"drop" : collection}

Removes all data for collection.



dropDatabase

{"dropDatabase" : 1}

Removes all data for the current database.



dropIndexes

{"dropIndexes" : collection, "index" : name}

Deletes the index named name from collection, or all indexes if name is "*".



getLastError

{"getLastError" : 1[, "w" : w[, "wtimeout" : timeout]]}

Checks for errors or other status information about the last operation performed on this connection. The command will optionally block until w slaves have replicated the last operation on this connection (or until timeout milliseconds have gone by).



isMaster

{"isMaster" : 1}

Checks if this server is a master or slave.



listCommands

{"listCommands" : 1}

Returns a list of all database commands available on this server, as well as some information about each command.



listDatabases

{"listDatabases" : 1}

Admin-only command listing all databases on this server.



ping

{"ping" : 1}

Checks if a server is alive. This command will return immediately even if the server is in a lock.



renameCollection

{"renameCollection" : a, "to" : b}

Renames collection a to b, where both a and [i]b[/i] are [i]full collection namespaces[/i] (e.g., "foo.bar" for the collection [i]bar[/i] in the [i]foo[/i] database).



repairDatabase

{"repairDatabase" : 1}

Repairs and compacts the current database, which can be a long-running operation.



serverStatus

{"serverStatus" : 1}

Gets administrative statistics for this server.


Remember, there are far more supported commands than just those listed earlier. Others are documented as appropriate throughout the rest of the book, and for the full list, just run listCommands.

MongoDB: The Definitive Guide

Learn more about this topic from MongoDB: The Definitive Guide.

Discover how MongoDB can help you manage a huMONGOus amount of data collected through your web application. With this authoritative introduction, you'll learn the many advantages of using document-oriented databases, and discover why MongoDB is a reliable, high-performance system that allows for almost infinite horizontal scalability. Written by engineers from 10gen, the company that develops and supports this open source database, MongoDB: The Definitive Guide provides guidance for database developers, advanced configuration for system administrators, and an overview of the concepts and use cases for other people on your project.

See what you'll learn


Tags:
0 Subscribe


0 Replies