mongoDB基础语句

mongo增删改查show dbs 显示所有dbuse fengDb 转换到哪个db,没有这个db就自动新建一个这个dbdb 显示当前操作的dbdb.fengDb.insert({"name":"fengnovo"}) 在fengDb上增加一个记录db.dropDatabase() 删除当前db(fengDb)use fengDb 将操作对象转到dbshow collections 显示上面的db即fengDb这个数据库下所有的集合 db.printCollectionStats() #查看各collection的状态 db.user1.drop() 删除fengDb数据库下面的user1集合 show users 查看所有用户 db.addUser('admin','pwd') #增加或修改用户密码
db.user1.save({'name':'fengnovo','age':26,'favor':['singing','playing']})#存储嵌套的对象
db.user1.save({'name':'fengnovo','age':27})#存储数组对象
db.user1.update({'name':'fengnovo'},{'$set':{'age':27}},upsert=true,multi=true)
#根据query条件修改,如果不存在则插入,允许修改多条记录
db.user1.remove({'age':24})#删除age=24的记录
db.user1.remove()#删除所有的记录
db.user1.find()#查找所有
db.user1.find().pretty()#查找所有并格式化在控制台
db.user1.findOne({'name':'fengnovo'})#查找一条记录
db.user1.find({'name':'fengnovo'}).limit(10) #根据条件检索10条记录
db.user1.find({'name':'fengnovo'}).sort({'age': 1})按age正序
db.user1.find({'name':'fengnovo'}).sort({'age': -1})按age
倒序
db.user1.find().sort({'age': -1}).limit(1)#sort排序
db.user1.count()#count操作 ,user1集合的数目
db.user1.distinct('age')#distinct操作 ,只取age的值
db.user1.find({"age": {‘$gte' : 27}}) #>=操作
db.user1.find({"age": {'$lte' : 27}}) #<=操作
db.user1.find({'address.city':'beijing'}) #子对象的查找
【mongoDB基础语句】下面的节选自http://hackpro.iteye.com/blog/1278105
db.help()
1. 超级用户相关:
#增加或修改用户密码
db.createUser({user:'admin',pwd: '123456',roles:[{role:'userAdmin',db:'admin'}]})
#查看用户列表
db.system.users.find()
#用户认证
db.auth('admin','pwd')
#删除用户
db.removeUser('mongodb')
#查看所有用户
how user
#查看所有数据库
how d
#查看所有的collectio
how collectio
#查看各collection的状态
db.printCollectionStats()
#查看主从复制状态
db.printReplicationInfo()
#修复数据库
db.repairDatabase()
#设置记录profiling,0=off 1=slow 2=all
db.setProfilingLevel(1)
#查看profiling
how profile
#拷贝数据库
db.copyDatabase('mail_addr','mail_addr_tmp')
#删除collectio
db.mail_addr.drop()
#删除当前的数据库
db.dropDatabase()
2. 客户端连接
/usr/local/mongodb/bin/mongo user_addr -u user -p 'pwd’
3. 索引
增加索引: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’)
4. 管理
#查看collection数据的大小
db.deliver_status.dataSize()
#查看colleciont状态
db.deliver_status.stats()
#查询所有索引的大小
db.deliver_status.totalIndexSize()

    推荐阅读