首页 技术 正文
技术 2022年11月17日
0 收藏 887 点赞 2,469 浏览 3986 个字

mongo模块
mongo — 客户端,连接到mongo数据库
mongod — 服务端,启动mongo服务,默认占领27017端口
mongodump — 数据库备份
mongorestore — 数据库恢复
bsondump — 数据导出成bson结构
mongoexport — 数据库导出成json文档或csv文档
mongos — 路由器,用于数据库集群、分片

mongo启动
mongod –dbpath xx –logpath xx –fork –port 27017
dbpath — 数据存储路径
logpath — 日志存储路径
fork — 后台运行
port — 端口号 默认27017
smailfiles 可以最小的启动mongod服务(占领400M左右磁盘空间)

mongo db数据库
show dbs — 查看当前数据库
use [dbName] — 显示创建或使用该数据库
db.dropDatabase(); — 删除当前数据库

mongo collections表
show collections — 查看当前所有collection
db.createCollection([collectionName]); — 显示创建collection
db.[collectionName].insert(); — 隐式创建collection
db.[collectionName].drop(); — 删除collection

mongo 增删改查
{db: ‘test’, collection: ‘stu’}
use test;
— db.stu.insert(document)
db.stu.insert({_id: ‘1’, sid: ‘001’, name: ‘xiaoming’}) // 添加单行数据
db.stu.insert([{},{},{}]) // 添加多行数据
— db.stu.remove(query, options)
db.stu.remove() // 删除collection所有数据
db.stu.remove({name: ‘xiaoming’}) // 删除全部指定数据
db.stu.remove({name: ‘xiaoming’}, true) // 删除一条指定数据
— db.stu.update(query, update, options<upsert, multi>)
db.stu.update({sid: ‘001’}, {name: ‘xiaohong’}) // 替换数据
db.stu.update({sid: ‘001’}, {$set:{name: ‘xiaohong’}}) // 更新1条数据
db.sud.update({name: ‘xiaoming’}, {$set:{name: ‘xiaohong’}, {multi: true}}}) //更新多条数据
<$set $unset $rename $inc $setOnInsert> <upsert-没有匹配直接插入 multi多行>
— db.stu.find(query, content)
db.stu.find() // 查询所有数据
db.stu.find({sid: ‘001’}) // 查询指定数据
db.stu.find({sid: ‘001’}, {_id: 0, name: 1}) // 查询指定数据显示特定属性
— 更多查询条件
<$ne $lt(e) $gt(e) $in $nin>
<$all $exist $mod $type> 
<$where $regex>
db.stu.find({_id: ‘1’, name: {$ne: ‘xiaoming’}}) // and查询
db.stu.find({$or: [{_id:’1′}, {id: {$lt:2}}]}) // or查询
db.stu.find({_id: ‘1’, $or: [{name: ‘x’}, {name: ‘xx’}]}) // and+or查询
db.stu.find({$nor: [{}, {}]}) // nor查询
db.stu.find({$where: “this.name==’xiaoming'”})
db.stu.find({name: {$regex: ‘.*’}})
<.skip() .limit()>

mongo cursor游标
var cursor = db.[collectionName].find(query);
cursor -> <next()、hasNext()>
cursor.forEach(function(obj) {printjson(obj)})

mongo index索引 <btree二叉树 hash哈希-两种索引方案>
创建索引– db.[collectionName].ensureIndex(index, options)
ensureIndex({name: [1/-1/’hash’]}) // 单列索引
ensureIndex({name: 1, age: 1}) // 多列索引
ensureIndex({‘grade.math’: 1}) // 子索引
ensureIndex({name: 1}, {unique: true, sparse: true}) // 唯一索引、稀疏索引(包含索引字段的数据才建立索引)
删除索引– db.[collectionName].dropIndex(index) | dropIndexes()
修复索引– db.[collectionName].reIndex()
显示索引– db.[collectionName].getIndexes()

mongo auth用户管理
后台启动服务时添加 –auth 启动权限验证
添加超级用户 use admin -> db.addUser(‘user’, ‘pwd’, ‘true/false-可读/可写’)
登录时认证 db.auth(‘user’, ‘pwd’)
修改密码 db.changeUserPassword(‘user’, ‘newpwd’)
删除用户 db.removeUser(‘user’)

mongo 备份恢复
mongoimport/mongoexport – mongodump/mongorestore
–导出json、csv格式及恢复(使用不同数据库数据转移)
mongoexport -d dbName -c collectionName -f 列名 -q query条件 [–csv] -o 文件位置
mongoexport -d test -c stu -f sid,name -q ‘{name:’xiaoming’}’ [–csv] -o ./test.stu.[json/csv]
mongoimport -d test -c stu –type json –file ./test.stu.json
mongoimport -d test -c stu -f sid,name –type csv –headerline –file ./test.stu.csv
–导出bson格式及恢复(同时导出bson数据文件和json索引文件,体积更小,包含索引信息)
mongodump [-d -c -f -o]
mongorestore [-d -c -f –directoryperdb dump/test]

mongo 复制集
实现步骤:
cd /usr/local/var/
mkdir mongodb/m17 mongodb/m18 mongodb/m19
mongod –dbpath mongodb/m17/ –logpath log/mongodb/m17.log –logappend –port 27017 –fork –smallfiles –replSet rsTest
mongod –dbpath mongodb/m17/ –logpath log/mongodb/m18.log –logappend –port 27018 –fork –smallfiles –replSet rsTest
mongod –dbpath mongodb/m17/ –logpath log/mongodb/m19.log –logappend –port 27019 –fork –smallfiles –replSet rsTest
mongo –port 27017
use admin
var rsconfig = {
_id: ‘rsTest’,
members: [
{_id: 0, host: ‘localhost:27017’},
{_id: 1, host: ‘localhost:27018’},
{_id: 2, host: ‘localhost:27019’}
]
}
rs.initiate(rsconfig)
— 以上已完成复制集相关配置
— 以下为加入auth、keyfile及mongoose接入
keyfile创建–
openssl rand -base64 64 > keyfile.dat #生成64字节密钥文件
chmod 600 keyfile.dat 设置权限
将keyfile文件复制到所有节点下
用户权限设置–
use admin
db.createUser({user:’root’,pwd:’123456′,roles:[‘root’]}) #超级用户
use test
db.createUser({user:’test’,pwd:’123456′,roles:[{role:’readWrite’,db:’test’}]})
mongo.conf文件配置–
keyfile=’./keyfile.dat’
auth=’true’
mongoserver启动及rs相关操作–
mongod -f xx.conf
use admin
db.auth(‘root’, ‘123456’)
rs.status()
rs.slaveOk()
rs.conf()
rs.add()
rs.remove()
rs.reconfig()
db.shutdownServer()
mongoose可以传多个url并设置replSet的值’rstest’

mongo 分片

~持续补上更多.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922