生活随笔
收集整理的這篇文章主要介紹了
MongoDB常用的操作命令(转)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
超級用戶相關:
use admin #增加或修改用戶密碼 db.addUser(ixigua,'pwd')#查看用戶列表db.system.users.find() #用戶認證db.auth(ixigua,'pwd')#刪除用戶db.removeUser('mongodb')#查看所有用戶show users#查看所有數據庫show dbs#查看所有的collectionshow collections#查看各collection的狀態db.printCollectionStats()#查看主從復制狀態db.printReplicationInfo()#修復數據庫db.repairDatabase()#設置記錄profiling,0=off 1=slow 2=all db.setProfilingLevel(1)#查看profilingshow profile#拷貝數據庫db.copyDatabase('mail_addr','mail_addr_tmp')#刪除collectiondb.mail_addr.drop()#刪除當前的數據庫db.dropDatabase() 客戶端連接
/usr/local/mongodb/bin/mongo 8.8.88/ixigualib -u ixigua -p 'pwd' 增刪改
#存儲嵌套的對象db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})#存儲數組對象db.user_addr.save({'Uid':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']})#根據query條件修改,如果不存在則插入,允許修改多條記錄db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)#刪除yy=5的記錄db.foo.remove({'yy':5})#刪除所有的記錄db.foo.remove() 索引
#增加索引:1(ascending),-1(descending) db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});#索引子對象db.user_addr.ensureIndex({'Al.Em': 1})#查看索引信息 db.deliver_status.getIndexes() db.deliver_status.getIndexKeys() #根據索引名刪除索引 db.user_addr.dropIndex('Al.Em_1') 查詢
#查找所有 db.foo.find() #查找一條記錄 db.foo.findOne() #根據條件檢索10條記錄 db.foo.find({'msg':'Hello 1'}).limit(10) #sort排序 db.deliver_status.find({'From':'ixigua@sina.com'}).sort({'Dt',-1})db.deliver_status.find().sort({'Ct':-1}).limit(1)#count操作db.user_addr.count()#distinct操作 db.foo.distinct('msg') #>操作 db.foo.find({"timestamp": {"$gte" : 2}}) #子對象的查找 db.foo.find({'address.city':'beijing'}) 管理
#查看collection數據的大小 db.deliver_status.dataSize() #查看colleciont狀態 db.deliver_status.stats() #查詢所有索引的大小 db.deliver_status.totalIndexSize()
總結
以上是生活随笔為你收集整理的MongoDB常用的操作命令(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。