日期:2014-05-16  浏览次数:20436 次

Mongodb常用命令介绍

查看命令的方式:

1.在shell中运行db.listCommands()

2.在浏览器中访问管理员接口:http://ipaddress:28017/_commands

?

下面介绍在Mongodb中最经常使用的命令,具体如下:

命令:buildInfo

格式:{"buildInfo":1}

介绍:管理专用命令,返回Mongodb服务器的版本号和主机的操作系统。

示例:

> 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 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_41",
	"versionArray" : [
		2,
		0,
		6,
		0
	],
	"bits" : 32,
	"debug" : false,
	"maxBsonObjectSize" : 16777216,
	"ok" : 1
}

命令:collStats

格式:{"collStats":collection}

介绍:返回指定集合的统计信息,包括数据大小、已分配的存储空间和索引的大小。

示例:

> 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
}

命令:distinct

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

介绍:列出指定集合中满足查询条件的文档的指定键的所有不同值

示例:?

> 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
}

命令:drop

格式:{"drop":collection}

介绍:删除集合的所有数据

示例:?

> 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() #再次查询,结果为空

命令:dropDatabase

格式:{"dropDatabase":1}

介绍:删除当前数据库中的所有数据

示例:略

?

命令:dropIndexes

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

介绍:删除集合里面名称为name的索引,如果名称为"*",则删除全部索引。

示例:

> 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 }

命令:findAndModify

格式:

介绍:查找并修改

示例:?