mongodb命令小结

常用基础命令

# 显示数据库
show dbs;

# 显示集合
show collections;

# 显示创建集合
db.createCollection('coll_name')

# 隐式创建
db.coll.insert({})  # coll不存在就会创建

# 插入记录
db.coll.insert({})  # bson 格式

# 删除记录
db.coll.remove({})

# 查询
# 查询所有
db.coll.find({})

# 格式化输出
db.coll.find().pretty()

# 查询单条
db.coll.findOne()

# 根据条件查询
db.coll.find({})

# 查询年龄大于20
db.coll.find({age:{$gt:20}})

# 查询大于等于20
db.coll.find({age:{$gte:20}})

# 查询不等于20
db.coll.find({age:{$ne:20})

# 分页查询,查询前20条
db.coll.find().skip(0).limit(20)

排序,-1表示降序,1表示升序
db.coll.find().sort({f1:-1,f2:1}).skip(start).limit(limit_number)

# 查询文档集合的包含关系,用$all
# 查询文档中的字段包含值,比如查询一个文档中的字段为hobby,查询hobby为篮球和足球的文档数据
db.coll.find({hobby:{$all:['篮球','足球']}})

# 与上面相反,使用$nin,即文档中的某一个字段数组不包含指定的值的数据
db.coll.find({hobby:{$nin:['篮球','足球']}})

# 使用 $or
db.coll.find({$or:[{condition1:v1},{condition2,:v2},{},....]})

# 使用 $nor
db.coll.find({$nor:[{condition1:v1},{condition2,:v2},{},....]})

# 使用 $exist,查询文档中的字段是否存在
db.coll.find({f1:{$exist:1}})

mongo命令操作细节

//更新   update tableName  set   ""  where conditon
db.collectionName("condition","objectValue",upset,mutil)
四个参数如下:
1:用于设置更新的条件
2:用于设置更新的内容的对象
3:如果记录已经存在,更新它,否则新增一个记录,取值为0或1
4:如果有多条记录被满足,是否全部更新.. (1更新全部)(0更新1条)

// 删除集合文档当中的某一个键
db.coll.update({f1:v1,...},{$unset:{del_f1:1}},0,1)

// 更新所有的整数字段进行自增或者自减
db.coll.update({f1:v1,..},{$inc:{age:1}},0,1)
db.coll.update({f1:v1,..},{$inc:{age:10}},0,1)
db.coll.update({f1:v1,..},{$inc:{age:-1}},0,1)
db.coll.update({f1:v1,..},{$inc:{age:-8}},0,1)

# 游标
var cursor = db.coll.find()
cursor.next()
cursor.hasNext()

# 查看数据库帮助命令
db.help()       

# 查看系统索引
db.system.indexes.find()

# 固定集合:事先创建并且大小固定的集合
固定集合很像环形队列,如果空间不足,最早的文档就会被删除,
为新的文档腾出空间。一般来说,固定集合适用于任何想要自动淘汰过期属性的场景,没有太多的操作限制

db.createCollection("collectionName",{capped:true,size:10000,max:1000})
size:数据库存储硬盘上的文件大小
max:表示记录的数据文档个数

mongo线上操作索引

# 目标是删除索引重新建立索引
# 查询索引信息
db.collections.getIndexes();

# 删除指定的索引
db.collections.dropIndex(index_name)

# 重建索引
db.collections.ensureIndex({field:1,filed2:1}, {"name": "idex_name", "background": true, "unique": true, ...})
# 如果重建索引过程中有错误,请核实下重建索引规则,例如上述重建是保证f1和f2是唯一的

============索引命令小结============
// 索引
db.coll.find({}).explain()  # 查询执行的消耗事件

# 建立普通索引,以后台方式进行建立而不会导致mongodb在读写过程中被阻塞致使CPU过高
db.coll.ensureIndex({f1:1},{background: true})

# 查看索引
db.coll.getIndexes()

# 删除索引
db.coll.dropIndex({f1:1})

# 唯一索引,重复添加将会报错
db.coll.ensureIndex({f1:1},{unique:true}})

mongo备份与恢复

# 将数据备份
mongodump -h dbhost -d dbname -o dbdir

# 将数据恢复
mongorestore -h dbhost -d dbname -directoryperdb dbdir

# 导入与导出

# 导出数据库集合
mongoexport -h dbhost -d dbname -c collectionname -o ouput

# 将集合导入某个数据库的集合中
mongoimport -h dbhost -d dbname -c collectionname dbdir

## 线上操作
### mongodb按条件导出数据表
mongoexport --help # 查看使用
mongoexport --host=127.0.0.1 --port=30000 --username=user --password=pwd --db=cc_pt_app --collection=gamelive_uid_date_livetime -q '{"name": "keithl"}' -o output.log

### mongodb导入json数据
mongoimport --host=192.168.229.148 --port=30000 --username=gamble --password=gamblepwd --db=gamble --collection=gamelive_anchor_current_signing --file input.json

### 备份
mongodump3.0.14 --host=127.0.0.1 --port=30000 --username=user --password=pwd --db=cc_pt_app --collection=gamelive_uid_date_livetime --out=mongo_gamelive_uid_date_livetime_backup_20180525
发布了63 篇原创文章 · 获赞 9 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/wind_602/article/details/90374779
今日推荐