天天看點

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()

本文轉自 不得閑 部落格園部落格,原文連結: http://www.cnblogs.com/DxSoft/archive/2010/10/21/1857380.html

  ,如需轉載請自行聯系原作者

http://www.cnblogs.com/DxSoft/archive/2010/10/21/1857380.html