How to view commands:
1. Run Db.listcommands () in the shell
2. Accessing the administrator interface in the browser: http://ipaddress:28017/_commands
The following are the most frequently used commands in MongoDB, as follows:
Command: Buildinfo
Format: {"Buildinfo": 1}
Description: Manages a dedicated command that returns the version number of the MongoDB server and the host's operating system.
Example:
Shell Code
> Db.runcommand ({"Buildinfo": 1})
{
"Version": "2.0.6",
"Gitversion": "E1C0CBC25863F6356AA4E31375ADD7BB49FB05BC",
"SysInfo": "Linux domu-12-31-39-01-70-b4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 12:39:36 EST. i686 boost_lib_version=1_4 1 ",
"Versionarray": [
2,
0,
6,
0
],
"Bits": 32,
"Debug": false,
"Maxbsonobjectsize": 16777216,
"OK": 1
}
Command: Collstats
Format: {"Collstats": Collection}
Description: Returns statistics for the specified collection, including the size of the data, the allocated storage space, and the size of the index.
Example:
Shell Code
> Db.runcommand ({"Collstats": "Users"})
{
"NS": "Test.users",
"Count": 3,
"Size": 508,
"Avgobjsize": 169.33333333333334,
"Storagesize": 4096,
"Numextents": 1,
"Nindexes": 2,
"Lastextentsize": 4096,
"Paddingfactor": 1.51,
"Flags": 0,
"Totalindexsize": 16352,
"Indexsizes": {
"_id_": 8176,
"Name_1": 8176
},
"OK": 1
}
Command: Distinct
Format: {"DISTINCT": Collection, "Key": Key, "Query": query}
Description: Lists all the different values for the specified key of the document in the specified collection that satisfy the query criteria
Example:
Shell Code
> Db.runcommand ({"distinct": "foo", "Key": "Name", "query": {"age": {"$GT": 20}})
{
"Values": [
"Gongyong",
"Chenzhou",
"Yixin"
],
"Stats": {
"N": 4,
"Nscanned": 6,
"Nscannedobjects": 6,
"Timems": 50,
"Cursor": "Basiccursor"
},
"OK": 1
}
Command: Drop
Format: {"Drop": Collection}
Description: Delete all data for a collection
Example:
Shell Code
> Db.bbb.save ({"X": 1, "Y": 2})
#先往bbb中存一条记录
> Db.bbb.find () #查询bbb中的数据
{"_id": ObjectId ("5027d919831a10b0f6e61385"), "X": 1, "Y": 2}
#使用drop命令删除bbb集合中的数据
> Db.runcommand ({"Drop": "BBB"})
{
"Nindexeswas": 1,
"MSG": "Indexes dropped for collection",
"NS": "TEST.BBB",
"OK": 1
}
> Db.bbb.find () #再次查询, the result is empty
Command: Dropdatabase
Format: {"Dropdatabase": 1}
Description: Deletes all data from the current database
Example: slightly
Command: dropindexes
Format: {"Dropindexes": Collection, "index": name}
Description: Deletes the index in the collection where the name is named, and if the name is called "*", all indexes are deleted.
Example:
Shell Code
> Db.system.indexes.find ()
{"V": 1, "key": {"_id": 1}, "ns": "Test.foo", "name": "_id_"}
{"V": 1, "key": {"_id": 1}, "ns": "Test.users", "name": "_id_"}
{"V": 1, "key": {"_id": 1}, "ns": "Test.games", "name": "_id_"}
{"V": 1, "key": {"_id": 1}, "ns": "Test.blog.post", "name": "_id_"}
{"V": 1, "key": {"_id": 1}, "ns": "Test.lists", "name": "_id_"}
{"V": 1, "key": {"_id": 1}, "ns": "Test.math", "name": "_id_"}
{"V": 1, "key": {"name": 1}, "ns": "Test.users", "name": "Name_1"}
{"V": 1, "key": {"_id": 1}, "ns": "Test.map", "name": "_id_"}
{"V": 1, "key": {"GPs": "2d"}, "ns": "Test.map", "name": "Gps_", "min": -180, "Max": 181}
> Db.runcommand ({"dropindexes": "Users", "index": "Name_1"})
{"Nindexeswas": 2, "OK": 1}
> Db.system.indexes.find ()
{"V": 1, "key": {"_id": 1}, "ns": "Test.foo", "name": "_id_"}
{"V": 1, "key": {"_id": 1}, "ns": "Test.users", "name": "_id_"}
{"V": 1, "key": {"_id": 1}, "ns": "Test.games", "name": "_id_"}
{"V": 1, "key": {"_id": 1}, "ns": "Test.blog.post", "name": "_id_"}
{"V": 1, "key": {"_id": 1}, "ns": "Test.lists", "name": "_id_"}
{"V": 1, "key": {"_id": 1}, "ns": "Test.math", "name": "_id_"}
{"V": 1, "key": {"_id": 1}, "ns": "Test.map", "name": "_id_"}
{"V": 1, "key": {"GPs": "2d"}, "ns": "Test.map", "name": "Gps_", "min": -180, "Max": 181}
Command: findandmodify
Format:
Description: Find and modify
Example:
Shell Code
> Db.foo.find ({"Name": "Yixin"})
{"_id": ObjectId ("5027d84b831a10b0f6e61384"), "name": "Yixin", "Age": 23}
> Db.runcommand ({"findandmodify": "foo",
... "Query": {"name": "Yixin"},
... "Update": {"$inc": {"Age": 1}}) #把name为yixin的记录中age值加1
{
"Lasterrorobject": {
"Updatedexisting": true,
"N": 1,
"ConnectionID": 2,
"Err": null,
"OK": 1
},
' Value ': {
"_id": ObjectId ("5027d84b831a10b0f6e61384"),
"Name": "Yixin",
"Age": 23
},
"OK": 1
}
> Db.foo.find ({"Name": "Yixin"})
{"_id": ObjectId ("5027d84b831a10b0f6e61384"), "name": "Yixin", "Age": 24}
Command: GetLastError
Format: {"GetLastError": 1[, "W": w[, "wtimeout": timeout}
Description: View the error message or other status information for the last operation performed on this collection. This command blocks until the W server replicates the last operation of the collection.
Example:
Shell Code
> Db.runcommand ({"GetLastError": 1})
{"n": 0, "ConnectionID": 2, "err": null, "OK": 1}
Command: IsMaster
Format: {"IsMaster": 1}
Description: Check whether this server is the primary or the server
Example:
Shell Code
> Db.runcommand ({"IsMaster": 1})
{"IsMaster": True, "maxbsonobjectsize": 16777216, "OK": 1}
Command: Listcommands
Format: {"Listcommands": 1}
Description: Returns all commands and related information that can be run on the server.
Example: Returning too many results, the example omits the
Command: listdatabases
Format: {"listdatabases": 1}
Description: Manage dedicated commands to list all databases on the server
Example:
Shell Code
> Db.runcommand ({"ListDatabases": 1})
{"ErrMsg": "Access denied; Use admin db "," OK ": 0}
#报错, prompting access is denied, requires the admin database to be used
> Use admin #切换到admin数据库
Switched to DB admin
> Db.runcommand ({"ListDatabases": 1})
{
"Databases": [
{
"Name": "Test",
"Sizeondisk": 67108864,
"Empty": false
},
{
"Name": "Results",
"Sizeondisk": 67108864,
"Empty": false
},
{
"Name": "Admin",
"Sizeondisk": 1,
"Empty": True
},
{
"Name": "Local",
"Sizeondisk": 1,
"Empty": True
}
],
"TotalSize": 134217728,
"OK": 1
}
Command: Ping
Format: {"ping": 1}
Description: Check that the server link is healthy. Even if the server is locked, this command will return immediately.
Example:
Shell Code
> Db.runcommand ({"Ping": 1})
{"OK": 1}
Command: Renamecollection
Format: {"Renamecollection": A, "to": B}
Description: Rename collection A to B, where both A and B must be full collection namespaces (for example, "Test.foo" represents the Foo collection in the test database)
Example:
Shell Code
> Db.runcommand ({"renamecollection": "foo", "to": "Foo_bak"})
{"ErrMsg": "Access denied; Use admin db "," OK ": 0}
> Use admin
Switched to DB admin
> Db.runcommand ({"renamecollection": "foo", "to": "Foo_bak"})
{
"ErrMsg": "Exception:source namespace does not exist",
"Code": 10026,
"OK": 0
}
> Db.runcommand ({"Renamecollection": "Test.foo", "to": "Test.foo_bak"})
{"OK": 1}
> Db.foo_bak.find ()
> Use test
Switched to DB test
> Db.foo_bak.find ()
{"_id": ObjectId ("502149203e2ff9961cc7b555"), "name": "Chenzhou"}
{"_id": ObjectId ("502149353e2ff9961cc7b556"), "Age": 23}
{"_id": ObjectId ("5021495c3e2ff9961cc7b557"), "name": "Gongyong", "Age": 26}
{"_id": ObjectId ("50214ad63e2ff9961cc7b558"), "name": "Chenzhou", "Age": $, "Friends": $, "enemies": 2}
{"_id": 123, "X": 1}
{"_id": ObjectId ("5027d84b831a10b0f6e61384"), "name": "Yixin", "Age": 24}
Command: RepairDatabase
Format: {"RepairDatabase": 1}
Description: Repairs and compresses the current database, which can be time-consuming.
Example: slightly
Command: Serverstatus
Format: {"Serverstatus": 1}
Description: Returns administrative statistics for this server.
Example:
Shell Code
> Db.runcommand ({"Serverstatus": 1})
{
"Host": "Localhost.localdomain",
"Version": "2.0.6",
"Process": "Mongod",
"Uptime": 3661,
"Uptimeestimate": 2987,
"LocalTime": Isodate ("2012-08-12t17:07:05.076z"),
"GlobalLock": {
"TotalTime": 3659420009,
"LockTime": 609397,
"Ratio": 0.00016652830188971076,
"Currentqueue": {
"Total": 0,
"Readers": 0,
"Writers": 0
},
"Activeclients": {
"Total": 0,
"Readers": 0,
"Writers": 0
}
},
"Mem": {
"Bits": 32,
"Resident": 45,
"Virtual": 158,
"Supported": TRUE,
"Mapped": 64
},
"Connections": {
"Current": 1,
"Available": 818
},
"Extra_info": {
"Note": "Fields vary by platform",
"Heap_usage_bytes": 491848,
"Page_faults": 0
},
"Indexcounters": {
"Btree": {
"Accesses": 1,
"Hits": 1,
"Misses": 0,
"Resets": 0,
"Missratio": 0
}
},
"Backgroundflushing": {
"Flushes": 60,
"Total_ms": 119,
"Average_ms": 1.9833333333333334,
"Last_ms": 0,
"Last_finished": Isodate ("2012-08-12t17:06:05.816z")
},
"Cursors": {
"Totalopen": 0,
"Clientcursors_size": 0,
"TimedOut": 0
},
"Network": {
"Bytesin": 7312,
"Bytesout": 63515,
"Numrequests": 102
},
"Opcounters": {
"Insert": 2,
"Query": 29,
"Update": 1,
"Delete": 0,
"Getmore": 0,
"Command": 75
},
"asserts": {
"Regular": 0,
"Warning": 0,
"MSG": 0,
"User": 1,
"Rollovers": 0
},
"writebacksqueued": false,
"OK": 1
}
MongoDB Common Commands Introduction