文章目录
0、操作账户
- 数据库用户角色:read、readWrite
- 数据库管理角色:dbAdmin、dbOwner、userAdmin
- 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager
- 备份恢复角色:backup、restore;
- 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、 dbAdminAnyDatabase
- 超级用户角色:root
- 间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、 serAdminAnyDatabase)
// 切换到 admin 数据库
use admin
// 内置角色
// read:允许用户读取指定数据库
// readWrite:允许用户读写指定数据库
// dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问 system.profile
// userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
// clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限
// readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
// readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
// userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
// dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限
// root:只在admin数据库中可用。超级账号,超级权限
// dbOwner:库拥有者权限,即readWrite、dbAdmin、userAdmin角色的合体
// 添加用户格式
db.createUser({
user: "账号",
pwd: "密码",
roles: [
{role: "角色", db: "安全认证的数据库实例名称"},
{role: "角色", db: "安全认证的数据库实例名称"}
]
})
// role=root,则db必须为admin库
db.createUser({
user: "myroot",
pwd: "root",
roles: [
{role: "root", db: "admin"}
]
})
// 修改密码
db.changeUserPassword('root', 'rootNew');
// 用户添加角色
db.grantRolesToUser('用户名', [{role: '角色名', db: '数据库名'}])
// 验证用户
// db.auth("账号", "密码")
db.auth("myroot", "root")
// 删除用户
// db.dropUser("账号")
db.dropUser("myroot")
// 以 auth 方式启动 mongod(也可以在mongo.conf 中添加auth=true 参数)
./bin/mongod -f conf/mongo.conf --auth
1、操作数据库
// 查看数据库
show dbs
// 切换数据库(如果没有对应的数据库则创建)
use td
// 删除当前数据库
db.dropDatabase()
2、操作集合
// 创建集合
db.createCollection("td_collection")
// 查看集合
show tables
show collections
// 删除集合
db.td_collection.drop();
3、操作数据 - 增
(1)插入单条数据
// db.集合名.insert(⽂档)
db.td_collection.insert({
name: "蓝田",
birthday: new ISODate("2000-07-01"),
expectSalary: 15000,
gender: 0,
city: "bj"
})
// db.集合名.insertOne(⽂档)
db.td_collection.insertOne({
name: "蓝田",
birthday: new ISODate("2000-07-01"),
expectSalary: 15000,
gender: 1,
city: "bj"
})
(2)插入多条数据
//
// db.集合名.insert([⽂档,⽂档])
db.td_collection.insert([{
name: "蓝田",
birthday: new ISODate("2000-07-01"),
expectSalary: 15000,
gender: 0,
city: "bj"
}, {
name: "CJ",
birthday: new ISODate("2000-07-01"),
expectSalary: 18000,
gender: 0,
city: "bj"
}])
// db.集合名.insertMany([⽂档,⽂档])
db.td_collection.insertMany([{
name: "蓝田",
birthday: new ISODate("2000-07-01"),
expectSalary: 15000,
gender: 0,
city: "bj"
}, {
name: "CJ",
birthday: new ISODate("2000-07-01"),
expectSalary: 18000,
gender: 0,
city: "bj"
}])
4、操作数据 - 查
(1)比较条件查询
// db.集合名.find()
db.td_collection.find() // 无条件查询
// db.集合名.find({条件})
db.td_collection.find({}) // 无条件查询
db.td_collection.find({city: "bj"}) // where 字段名=值
db.td_collection.find({expectSalary: 15000}) // where 字段名=值
db.td_collection.find({expectSalary: {$eq: 18000}}) // where 字段名=值
db.td_collection.find({expectSalary: {$gt: 10000}}) // where 字段名>值
db.td_collection.find({expectSalary: {$lt: 16000}}) // where 字段名<值
db.td_collection.find({expectSalary: {$gte: 18000}}) // where 字段名>=值
db.td_collection.find({expectSalary: {$lte: 15000}}) // where 字段名<=值
db.td_collection.find({expectSalary: {$ne: 15000}}) // where 字段名!=值
(2)逻辑条件查询
// and 且
// db.集合名.find({key1:value1, key2:value2}).pretty()
db.td_collection.find({city: "bj", expectSalary: {$eq: 15000}})
// db.集合名.find({$and:[{key1:value1}, {key2:value2}]}).pretty()
db.td_collection.find({$and: [{city: "bj"}, {expectSalary: 15000}]})
// or 或
// db.集合名.find({$or:[{key1:value1}, {key2:value2}]}).pretty()
db.td_collection.find({$or: [{city: "bj"}, {expectSalary: 15000}]})
// not 非
// db.集合名.find({key:{$not:{$操作符:value}}).pretty()
db.td_collection.find({city: {$not: {$eq: "bj"}}})
(3)排序
// db.集合名.find({条件}).sort({排序字段:排序⽅式}))
db.td_collection.find().sort({expectSalary: 1}) // 升序
db.td_collection.find().sort({expectSalary: -1}) // 降序
(4)分页查询
// db.集合名.find({条件}).sort({排序字段:排序⽅式})).skip(跳过的⾏数).limit(⼀⻚显示多少数据)
db.td_collection.find().sort({expectSalary: 1}).skip(1).limit(2)
5、操作数据 - 改
(0)语法
// 参数说明:
// query : update的查询条件,类似 sql update 查询内 where 后⾯的
// update : update的对象和⼀些更新的操作符(如$set,$inc...)等,类似 sql update 中 set 后⾯的
// $set :设置字段值
// $unset :删除指定字段
// $inc:对修改的值进行自增
// upsert : 可选,如果不存在update的记录,是否插⼊ objNew(true为插⼊,默认是false,不插⼊)
// multi : 可选,MongoDB 默认是false,只更新找到的第⼀条记录,如果为true,就把按条件查出来多条记录全部更新
// writeConcern :可选,⽤来指定mongod对写操作的回执行为,比如写的行为是否需要确认。
// db.集合名.update(
// <query>,
// <update>,
// {
// upsert: <boolean>,
// multi: <boolean>,
// writeConcern: <document>
// })
(1)multi
// db.集合名.update({条件},{$set:{字段名:值}},{multi:true})
// 命令行中有效,multi=true,只修改找到的第⼀条记录,multi=false,修改全部
db.td_collection.update(
{expectSalary: 18000},
{$set: {expectSalary: 28000}},
{
multi: true,
upsert: false
})
(2)updateOne、updateMany
// updateOne:每执行一次只修改找到的第⼀条记录
db.td_collection.updateOne(
{expectSalary: 18000},
{$set: {expectSalary: 28000}},
{
upsert: false
})
// updateMany:符合条件的都修改
db.td_collection.updateMany(
{expectSalary: 28000},
{$set: {expectSalary: 18000}},
{
upsert: false
})
// 测试修改效果
db.td_collection.find({expectSalary: 28000})
db.td_collection.find({expectSalary: 18000})
(3)没有找到符合条件的数据,且upsert=true
// 下面三个语句效果相同,只要没有找到符合条件的数据,且upsert: true,则会自动插入一条要设置的数据
db.td_collection.update(
{expectSalary: 88000},
{$set: {expectSalary: 15000}},
{
upsert: true
})
db.td_collection.updateOne(
{expectSalary: 88000},
{$set: {expectSalary: 15000}},
{
upsert: true
})
db.td_collection.updateMany(
{expectSalary: 88000},
{$set: {expectSalary: 15000}},
{
upsert: true
})
// 测试修改效果:会新增3条数据
db.td_collection.find({expectSalary: 15000})
(4)清空字段值
// update:清空一条
db.td_collection.update(
{name: "蓝田"},
{$unset: {expectSalary: ""}},
{
upsert: false
})
// updateOne:清空一条
db.td_collection.updateOne(
{name: "蓝田"},
{$unset: {expectSalary: ""}},
{
upsert: false
})
// updateMany:清空所有满足条件的
db.td_collection.updateMany(
{name: "蓝田"},
{$unset: {expectSalary: ""}},
{
upsert: false
})
// 测试修改效果
db.td_collection.find({name: "蓝田"})
// 找到满足条件 name: "蓝田" 的第一条数据,然后清空 expectSalary 之外的所有字段的值,并设置 expectSalary = 18000
db.td_collection.update(
{name: "蓝田"},
{expectSalary: 18000},
{
upsert: false
})
// 测试修改效果
db.td_collection.find({expectSalary: 18000})
6、操作数据 - 删
(1)deleteOne、deleteMany
db.td_collection.deleteOne({city: "bj"}) // 删除满足条件的1条数据
db.td_collection.deleteMany({city: "bj"}) // 删除满足条件的所有数据
(2)remove
// 参数说明:
// query :(可选)删除的⽂档的条件
// justOne : (可选)如果设为 true 或 1,则只删除⼀个⽂档,如果不设置该参数,或使⽤默认值 false,则删除所有匹配条件的⽂档
// writeConcern :(可选)⽤来指定mongod对写操作的回执⾏为
// db.collection.remove(
// <query>,
// {
// justOne: <boolean>,
// writeConcern: <document>
// })
// 删除满足条件的所有数据
db.td_collection.remove({expectSalary: 18000})
// 删除满足条件的第一条数据
db.td_collection.remove(
{city: "bj"},
{
justOne: true
})
// 删除满足条件的所有数据
db.td_collection.remove(
{city: "bj"},
{
justOne: false
})
7、操作数据 - 聚合
- 聚合允许通过转化合并由多个⽂档的数据来⽣成新的在单个⽂档⾥不存在的⽂档信息
- 聚合操作的输⼊是集中的⽂档,输出可以是⼀个⽂档也可以是多个⽂档
- ⼀般都是将记录按条件分组之后进⾏⼀系列求最⼤值,最⼩值,平均值的简单操作,也可以对记录进⾏复杂数据统计,数据挖掘的操作。
// 数据准备(8条数据)
db.createCollection("td_aggregation")
db.td_aggregation.insertOne({name:"张晓峰",birthday:new ISODate("2000-07-01"),gender:1,expectSalary:15000,city:"bj"})
db.td_aggregation.insertOne({name:"张震",birthday:new ISODate("1995-04-01"),gender:1,expectSalary:18000,city:"bj"})
db.td_aggregation.insertOne({name:"李山",birthday:new Date("1995-04-01"),gender:1,expectSalary:25000,city:"sh"})
db.td_aggregation.insertOne({name:"李笑来",birthday:new Date("1998-04-01 14:20:09"),gender:1,expectSalary:20000,city:"sh"})
db.td_aggregation.insertOne({name:null,birthday:new Date("1992-04-01 14:20:09"),gender:1,expectSalary:30000,city:"sh"})
db.td_aggregation.insertOne({birthday:new Date("1991-05-01 14:20:09"),gender:1,expectSalary:50000,city:"sz"})
db.td_aggregation.insert([{name:"李丽",birthday:new Date("1996-05-01 14:20:09"),gender:0,expectSalary:21000,city:"sz"}, {name:"李平",birthday:new Date("1997-07-01 14:20:09"),gender:0,expectSalary:22000,city:"sz"}])
(1)单目的聚合操作(Single Purpose Aggregation Operation)
// 计算满足条件的数量
db.td_aggregation.find().count()
db.td_aggregation.count()
// 查询去重后,满足条件的字段值
db.td_aggregation.distinct("city")
(2)聚合管道(Aggregation Pipeline)
- 聚合管道将MongoDB⽂档在⼀个管道处理完毕后将结果传递给下⼀个管道处理
- 管道操作是可以重复的
- MongoDB不允许Pipeline的单个聚合操作占⽤过多的系统内存,如果⼀个聚合操作消耗20%以上的内存,那么MongoDB直接停⽌操作,并向客户端输出错误消息。
// db.集合名称.aggregate(聚合操作)
// 聚合框架操作
// $group:将集合中的⽂档分组,可⽤于统计结果。
// $project:修改输⼊⽂档的结构。可以⽤来重命名、增加或删除域,也可以⽤于创建计算结果以及嵌套⽂档。
// $match:⽤于过滤数据,只输出符合条件的⽂档。$match使⽤MongoDB的标准查询操作。
// $limit:⽤来限制MongoDB聚合管道返回的⽂档数。
// $skip:在聚合管道中跳过指定数量的⽂档,并返回余下的⽂档。
// $sort:将输⼊⽂档排序后输出。
// $geoNear:输出接近某⼀地理位置的有序⽂档。
// 表达式:处理输⼊⽂档并输出(表达式只能⽤于计算当前聚合管道的⽂档,不能处理其它的⽂档)
// $sum:计算总和
// $avg:计算平均值
// $min:获取集合中所有⽂档对应值得最⼩值
// $max:获取集合中所有⽂档对应值得最⼤值
// $push:在结果⽂档中插⼊值到⼀个数组中
// $addToSet:在结果⽂档中插⼊值到⼀个数组中,但数据不重复
// $first:根据资源⽂档的排序获取第⼀个⽂档数据
// $last:根据资源⽂档的排序获取最后⼀个⽂档数据
// 按照 city 分组,统计每个 city 的值,放在一个数组中
db.td_aggregation.aggregate([{$group: {_id: "$city", city_name: {$push: "$city"}}}])
// 按照 city 分组,统计每个 city 中 expectSalary 的平均值
db.td_aggregation.aggregate([{$group: {_id: "$city", avgSal: {$avg: "$expectSalary"}}}])
db.td_aggregation.aggregate(
[
{$group: {_id: "$city", avgSal: {$avg: "$expectSalary"}}},
{$project: {city: "$city", avgSal: "$avgSal"}}
])
// 按照 city 分组,统计每个 city 出现的次数
db.td_aggregation.aggregate([{$group: {_id: "$city", city_count: {$sum: 1}}}])
db.td_aggregation.aggregate(
[
{$group: {_id: "$city", city_count: {$sum: 1}}},
{$match: {city_count: {$gte: 2}}}
])
(3) MapReduce 计算模型
- Pipeline 管道查询速度快于MapReduce,但是MapReduce的强⼤之处在于能够在多台Server上并⾏执⾏复杂的聚合逻辑。
- MapReduce是⼀种计算模型,简单的说就是将⼤批量的⼯作(数据)分解(MAP)执⾏,然后再将结果合并成最终结果(REDUCE)
// map:是JavaScript 函数,负责将每⼀个输⼊⽂档转换为零或多个⽂档,⽣成键值对序列,作为 reduce 函数参数
// reduce:是JavaScript 函数,对map操作的输出做合并的化简的操作(将key-value变成key-values,也就是把values数组变成⼀个单⼀的值value)
// out:统计结果存放集合
// query:⼀个筛选条件,只有满⾜条件的⽂档才会调⽤map函数。
// sort:和limit结合的sort排序参数(也是在发往map函数前给⽂档排序),可以优化分组机制
// limit:发往map函数的⽂档数量的上限(要是没有limit,单独使⽤sort的⽤处不⼤)
// finalize:可以对reduce输出结果再⼀次修改
// verbose:是否包括结果信息中的时间信息,默认为fasle
// 原理:Map 函数调⽤ emit(key, value), 遍历 collection 中所有的记录, 将 key 与 value 传递给 Reduce 函数进⾏处理
db.集合名称.mapReduce(
// map 函数
function () {
emit(key, value);
},
// reduce 函数
function (key, values) {
return reduceFunction
},
{
out: collection,
query: document,
sort: document,
limit: number,
finalize: <function>,
verbose: <boolean >
}
)
db.td_aggregation.mapReduce(
function () {
emit(this.city, this.expectSalary);
},
function (key, value) {
return Array.avg(value);
},
{
out: "cityAvgSal",
query: {expectSalary: {$gt: 15000}},
finalize: function (key, value) {
return value + 500;
},
verbose: true
}
)
db.cityAvgSal.find()
8、索引
8.1 创建索引
(1)单键索引(Single Field)
- MongoDB⽀持所有数据类型中的单个字段索引,并且可以在⽂档的任何字段上定义。
- 对于单个字段索引,索引键的排序顺序⽆关紧要,因为MongoDB可以在任⼀⽅向读取索引
// 数据准备
db.td_index.insertOne({name: "test1", salary: 18000})
db.td_index.insertOne({name: "test2", salary: 29000, birth: new ISODate("2000-01-01")})
// db.集合名.createIndex({"字段名":排序⽅式})
db.td_index.createIndex({name: 1})
// 创建索引并在后台运⾏
// db.集合名.createIndex({"字段":排序⽅式}, {background: true})
db.td_index.createIndex({name: 1}, {background: true})
// 旧语法
// db.集合名.ensureIndex({"字段名":排序⽅式})
db.td_index.ensureIndex({name: 1})
// 查看索引
db.td_index.getIndexes()
(2)过期索引 TTL(Time To Live)
- TTL索引是MongoDB中⼀种特殊的索引, 可以⽀持⽂档在⼀定时间之后⾃动过期删除
- ⽬前TTL索引只能在单字段上建⽴,并且字段类型必须是⽇期类型
// 数据准备
db.td_index.insertOne({name: "test1", salary: 18000})
db.td_index.insertOne({name: "test2", salary: 29000, birth: new ISODate("2000-01-01")})
// 特殊的单键索引
// db.集合名.createIndex({"⽇期字段":排序⽅式}, {expireAfterSeconds: 秒数})
db.td_index.createIndex({birth: 1}, {expireAfterSeconds: 5})
// 查看索引
db.td_index.getIndexes()
(3)复合索引(Compound Index)
- 复合索引⽀持基于多个字段的索引
- 复合索引要注意字段顺序与索引⽅向
// db.集合名.createIndex( { "字段名1" : 排序⽅式, "字段名2" : 排序⽅式 } )
(4)多键索引(Multikey indexes)
- 针对属性包含数组数据的情况,MongoDB⽀持针对数组中每⼀个element创建索引
- Multikey indexes⽀持strings、numbers、nested documents
(5)地理空间索引(Geospatial Index)
- 2dsphere 索引:⽤于存储和查找球⾯上的点
// 准备数据
db.td_index_2dsphere.insertOne(
{
loc: {type: "Point", coordinates: [116.482451, 39.914176]},
name: "大望路地铁",
category: "Parks"
}
)
db.td_index_2dsphere.insertOne(
{
loc: {type: "Point", coordinates: [116.492451, 39.934176]},
name: "test1",
category: "Parks"
}
)
db.td_index_2dsphere.insertOne(
{
loc: {type: "Point", coordinates: [116.462451, 39.954176]},
name: "test2",
category: "Parks"
}
)
db.td_index_2dsphere.insertOne(
{
loc: {type: "Point", coordinates: [116.562451, 38.954176]},
name: "test3",
category: "Parks"
}
)
db.td_index_2dsphere.insertOne(
{
loc: {type: "Point", coordinates: [117.562451, 37.954176]},
name: "test4",
category: "Parks"
}
)
// 2dsphere 索引,⽤于存储和查找球⾯上的点
db.td_index_2dsphere.ensureIndex({loc: "2dsphere"})
// 以 116.482451, 39.914176 为中心,找 0.05,即0.5公里内的点
db.td_index_2dsphere.find({
"loc": {
"$geoWithin": {
"$center": [[116.482451, 39.914176], 0.05]
}
}
})
// 计算中心点最近的三个点
db.td_index_2dsphere.aggregate([
{
$geoNear: {
near: {type: "Point", coordinates: [116.482451, 39.914176]},
key: "loc",
distanceField: "dist.calculated"
}
},
{$limit: 3}
])
// 查看索引
db.td_index_2dsphere.getIndexes()
- 2d 索引:⽤于存储和查找平⾯上的点
// 测试数据
db.td_index_2d.insertOne({"name": "Temple1", "tile": [32, 22]})
db.td_index_2d.insertOne({"name": "Temple2", "tile": [30, 22]})
db.td_index_2d.insertOne({"name": "Temple3", "tile": [28, 21]})
db.td_index_2d.insertOne({"name": "Temple4", "tile": [34, 27]})
db.td_index_2d.insertOne({"name": "Temple5", "tile": [34, 26]})
db.td_index_2d.insertOne({"name": "Temple6", "tile": [39, 28]})
// 2d 索引,⽤于存储和查找平⾯上的点
db.td_index_2d.ensureIndex({"tile": "2d"}, {"min": -90, "max": 90, "bits": 20})
// 查看
db.td_index_2d.find({"tile": {"$within": {"$box": [[0, 0], [30, 30]]}}})
// 查看索引
db.td_index_2d.getIndexes()
(6)全文索引(Text Index)
- ⽀持任意属性值为string或string数组元素的索引查询。
- ⼀个集合仅⽀持最多⼀个Text Index(中⽂分词不理想,推荐ES)
// 测试数据
db.td_index_text.insertOne({id: 1, name: "test1", description: "one world one dream in bj", city: "bj"})
db.td_index_text.insertOne({id: 2, name: "test2", description: "two world one dream in nj", city: "nj"})
db.td_index_text.insertOne({id: 3, name: "test3", description: "dj is not bj and nj", city: "dj"})
// db.集合.createIndex({"字段": "text"})
db.td_index_text.createIndex({description: "text"})
// db.集合.find({"$text": {"$search": "关键词"}})
db.td_index_text.find({"$text": {"$search": "two"}})
// 查看索引
db.td_index_text.getIndexes()
(7)哈希索引(Hashed Index)
- 针对属性的哈希值进⾏索引查询,当要使⽤Hashed index时,MongoDB能够⾃动的计算hash值,⽆需程序计算hash值
- hash index 仅⽀持等于查询,不⽀持范围查询
// db.集合.createIndex({"字段": "hashed"})
8.2 查看索引
// 获取针对某个集合的索引
db.td_index.getIndexes()
8.3 重建索引
// db.集合名.reIndex()
db.td_index.reIndex()
8.4 删除索引
// 根据索引名称删除
// db.集合名.dropIndex("索引名称")
db.td_index.dropIndex("INDEX-NAME")
// 删除该集合中所有索引(_id 对应的索引删除不了)
// db.集合名.dropIndexes()
db.td_index.dropIndexes()
8.5 获取索引大小
// db.集合名.totalIndexSize()
db.td_index.totalIndexSize()
9、数据备份 - mongodump
- 全量备份的三种方式:文件系统快照、复制数据文件、mongodump
- mongodump 命令
// -h:MongoDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:37017
// --db 或者 -d :需要备份的数据库实例
// -o:备份的数据存放位置,例如:/root/bdatas 在备份完成后,系统自动在root目录下建立一个bdatas目录,这个目录里面存放该数据库实例的备份数据
mongodump -h 服务器地址 -d 数据库实例名称 -o 备份数据存放位置
// 举例
// 备份所有 MongoDB 数据
mongodump --host 192.168.211.136 --port 37017
// 备份指定的数据库
mongodump --port 37017 --d dbname --out /data/backup/
// 将备份指定数据库的集合 td
mongodump --collection mycol -d td
// oplog 是replica set或者master/slave模式专用
// --oplog 选项只对全库导出有效,-d 和--oplog 不能同时使用
./bin/mongodump -h 127.0.0.1:37017 --oplog -o /root/bdatas
10、数据恢复 - mongorestore
- mongorestore 命令
// --host <:port>, -h <:port>:MongoDB所在服务器地址,默认为: localhost:37017
// --db 或者 -d :需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2
// --drop:恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用!
// <path>:mongorestore 最后的一个参数,设置备份数据所在位置,不能同时指定 <path> 和 --dir 选项,--dir也可以设置备份目录。
// 注意: 恢复指定的数据库 需要在恢复的路径中出现数据库的名字
// --dir:指定备份的目录,不能同时指定 <path> 和 --dir 选项
mongorestore -h <hostname><:port> -d dbname <path>
// 举例
./bin/mongorestore -h 127.0.0.1:37017 -d lg /root/bdatas/td
// --oplogReplay:可以重放oplog.bson中的操作内容
// --oplogLimit:回放的时间节点,即此时间之前的数据恢复,假设你后面有误操作,误操作的不恢复
mongorestore -h localhost:37017 --oplogReplay /root/dump
// 通过 oplog 查询误操作的最后时间
/root/mongodb/bin/bsondump oplog.rs.bson | grep ""op":"d"" | head
// 或
db.oplog.rs.find({"op" : "d"}).sort({"ts":-1})
总结
以上就是今天要讲的内容。