一、用户操作命令:

 1#增加或修改用户密码
 2db.addUser('admin','pwd')
 3#查看用户列表
 4db.system.users.find()
 5#用户认证
 6db.auth('admin','pwd')
 7#删除用户
 8db.removeUser('mongodb')
 9#查看所有用户
10show users
11#查看所有数据库
12show dbs
13#查看所有的collection
14show collections
15#查看各collection的状态
16db.printCollectionStats()
17#查看主从复制状态
18db.printReplicationInfo()
19#修复数据库
20db.repairDatabase()
21#设置记录profiling,0=off 1=slow 2=all
22db.setProfilingLevel(1)
23#查看profiling
24show profile
25#拷贝数据库
26db.copyDatabase('mail_addr','mail_addr_tmp')
27#删除collection
28db.mail_addr.drop()
29#删除当前的数据库
30db.dropDatabase() 

二、查询相关:

客户端连接

1/usr/local/mongodb/bin/mongo user_addr -u user -p 'pwd' 

增删改

 1#存储嵌套的对象
 2db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})
 3#存储数组对象
 4db.user_addr.save({'Uid':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']})
 5#根据query条件修改,如果不存在则插入,允许修改多条记录
 6db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)
 7#删除yy=5的记录
 8db.foo.remove({'yy':5})
 9#删除所有的记录
10db.foo.remove() 

索引相关:

 1增加索引:1(ascending),-1(descending)
 2db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
 3#索引子对象
 4db.user_addr.ensureIndex({'Al.Em': 1})
 5#查看索引信息
 6db.deliver_status.getIndexes()
 7db.deliver_status.getIndexKeys()
 8#根据索引名删除索引
 9db.user_addr.dropIndex('Al.Em_1')
10#查询所有索引的大小
11db.deliver_status.totalIndexSize()

查询相关:

 1查找所有
 2db.foo.find()
 3#查找一条记录
 4db.foo.findOne()
 5#根据条件检索10条记录
 6db.foo.find({'msg':'Hello 1'}).limit(10)
 7#sort排序
 8db.deliver_status.find({'From':'test@361way.com'}).sort({'Dt',-1})
 9db.deliver_status.find().sort({'Ct':-1}).limit(1)
10#count操作
11db.user_addr.count()
12#distinct操作
13db.foo.distinct('msg')
14#>操作
15db.foo.find({"timestamp": {"$gte" : 2}})
16#子对象的查找
17db.foo.find({'address.city':'beijing'})
18查看collection数据的大小
19db.deliver_status.dataSize()
20#查看colleciont状态
21db.deliver_status.stats()