MongoDB常用命令:
超級用戶相關: use admin
#增加或修改用戶密碼
db.addUser(ixigua,'pwd')
#查看用戶列表
db.system.users.find()
#用戶認證
db.auth(ixigua,'pwd')
#刪除用戶
db.removeUser('mongodb')
#查看所有用戶
show users
#查看所有數據庫
show dbs
#查看所有的collection
show collections
#查看各collection的狀態
db.printCollectionStats()
#查看主從復制狀態
db.printReplicationInfo()
#修復數據庫
db.repairDatabase()
#設置記錄profiling,0=off 1=slow 2=all
db.setProfilingLevel(1)
#查看profiling
show profile
#拷貝數據庫
db.copyDatabase('mail_addr','mail_addr_tmp')
#刪除collection
db.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':'
[email protected]','Al':['
[email protected]','
[email protected]']})
#根據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':'
[email protected]'}).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()
#查看當前所使用的數據庫
db
作者 mahout