2. #增加或修改用户密码
3. #查看用户列表
) |, v3 H! W+ t" a c5 ^4 b
4. #用户认证
5. #删除用户
6. #查看所有用户
7. #查看所有数据库
8. #查看所有的collection
2 P, r0 }5 Z8 t& Y) `: P0 ~ 9. #查看各collection的状态
- db.printCollectionStats()
复制代码 ! n2 B n( S* ~+ ?9 V4 ~) w8 y
10. #查看主从复制状态
- db.printReplicationInfo()
复制代码
# W+ {3 R/ e) a _1 `# Y 11. #修复数据库
12. #设置记录profiling,0=off 1=slow 2=all
13. #查看profiling
" e8 N H N( B( I8 l# E$ i& L6 O
14. #拷贝数据库
- db.copyDatabase('mail_addr','mail_addr_tmp')
复制代码
- |* {1 Y! A5 Y+ s. a. C; F, @ 15. #删除collection
16. #删除当前的数据库
2. 增删改
1. #存储嵌套的对象
- db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})
复制代码 / \6 R( G3 `1 |$ y9 B
2. #存储数组对象
- db.user_addr.save({'Uid':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']})
复制代码
1 z6 V9 z/ w- Q7 j" d% ~ 3. #根据query条件修改,如果不存在则插入,允许修改多条记录
- db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)
复制代码 9 B0 n+ b9 s6 _1 Z
4. #删除yy=5的记录
0 }2 t. @- y* O" N+ w; B* K 5. #删除所有的记录
3. 索引
1. #增加索引:1(ascending),-1(descending)
2. db.foo.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
3. #索引子对象
4. db.user_addr.ensureIndex({'Al.Em': 1})
5. #查看索引信息
6. db.foo.getIndexes()
7. db.foo.getIndexKeys()
8. #根据索引名删除索引
9. db.user_addr.dropIndex('Al.Em_1')
4. 查询
1. #查找所有
2. db.foo.find()
3. #查找一条记录
4. db.foo.findOne()
5. #根据条件检索10条记录
6. db.foo.find({'msg':'Hello 1'}).limit(10)
7. #sort排序
8. db.deliver_status.find({'From':'ixigua@sina.com'}).sort({'Dt',-1})
9. db.deliver_status.find().sort({'Ct':-1}).limit(1)
10. #count操作
11. db.user_addr.count()
12. #distinct操作,查询指定列,去重复
13. db.foo.distinct('msg')
14. #”>=”操作
15. db.foo.find({"timestamp": {"$gte" : 2}})
16. #子对象的查找
17. db.foo.find({'address.city':'beijing'})
5. 管理
1. #查看collection数据的大小
2. db.deliver_status.dataSize()
3. #查看colleciont状态
4. db.deliver_status.stats()
5. #查询所有索引的大小
6. db.deliver_status.totalIndexSize()
7 D g3 E2 n1 ~, K- H2 J7 `1 n
6. 高级查询
条件操作符 , S& l0 M: j0 t