MongoDB常用的操作命令

jopen 12年前發布 | 22K 次閱讀 MongoDB NoSQL數據庫

1.  超級用戶相關:
1. use admin
2. #增加或修改用戶密碼
3. db.addUser(ixigua,’pwd’)
4. #查看用戶列表
5. db.system.users.find()
6. #用戶認證
7. db.auth(ixigua,’pwd’)
8. #刪除用戶
9. db.removeUser(‘mongodb’)
10. #查看所有用戶
11. show users
12. #查看所有數據庫
13. show dbs
14. #查看所有的collection
15. show collections
16. #查看各collection的狀態
17. db.printCollectionStats()
18. #查看主從復制狀態
19. db.printReplicationInfo()
20. #修復數據庫
21. db.repairDatabase()
22. #設置記錄profiling,0=off 1=slow 2=all
23. db.setProfilingLevel(1)
24. #查看profiling
25. show profile
26. #拷貝數據庫
27. db.copyDatabase(‘mail_addr’,'mail_addr_tmp’)
28. #刪除collection
29. db.mail_addr.drop()
30. #刪除當前的數據庫
31. db.dropDatabase()
2. 客戶端連接
1. /usr/local/mongodb/bin/mongo 8.8.88/ixigualib -u ixigua -p ‘pwd’
3. 增刪改
1. #存儲嵌套的對象
2. db.foo.save({‘name’:'ysz’,'address’:{‘city’:'beijing’,'post’:100096},’phone’:[138,139]})
3. #存儲數組對象
4. db.user_addr.save({‘Uid’:'yushunzhi@sohu.com’,'Al’:['test-1@sohu.com','test-2@sohu.com']})
5. #根據query條件修改,如果不存在則插入,允許修改多條記錄
6. db.foo.update({‘yy’:5},{‘$set’:{‘xx’:2}},upsert=true,multi=true)
7. #刪除yy=5的記錄
8. db.foo.remove({‘yy’:5})
9. #刪除所有的記錄
10. db.foo.remove()
4. 索引
1. #增加索引:1(ascending),-1(descending)
2. db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
3. #索引子對象
4. db.user_addr.ensureIndex({‘Al.Em’: 1})
5. #查看索引信息
6. db.deliver_status.getIndexes()
7. db.deliver_status.getIndexKeys()
8. #根據索引名刪除索引
9. db.user_addr.dropIndex(‘Al.Em_1′)
5. 查詢
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’})
6. 管理
1. #查看collection數據的大小
2. db.deliver_status.dataSize()
3. #查看colleciont狀態
4. db.deliver_status.stats()
5. #查詢所有索引的大小
6. db.deliver_status.totalIndexSize()

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!