注:本文基于MongoDB 4.2.6编写
> db.bb.find().pretty()
{
"_id" : ObjectId("5ed6549830571733ccb3d678"),
"jordan" : 23,
"haha" : "ending"
}
{
"_id" : ObjectId("5ed6549830571733ccb3d679"),
"kobe" : 24,
"haha" : "ending"
}
> db.bb.remove({jordan: 23})
WriteResult({ "nRemoved" : 1 })
> db.bb.find().pretty()
{
"_id" : ObjectId("5ed6549830571733ccb3d679"),
"kobe" : 24,
"haha" : "ending"
}
删除某个文档首先需要匹配到这个文档,而对于匹配规则来说,和update()以及find()都是一致的。
不过需要注意的是,remove()会删除所有匹配的文档,
> db.bb.find().pretty()
{
"_id" : ObjectId("5ed6549830571733ccb3d679"),
"kobe" : 24,
"haha" : "ending"
}
{
"_id" : ObjectId("5ed6549830571733ccb3d67a"),
"tmac" : 1,
"haha" : "ending"
}
{
"_id" : ObjectId("5ed6554230571733ccb3d67b"),
"name" : "jay",
"hello" : "ball",
}
> db.bb.remove({"haha" : "ending"})
WriteResult({ "nRemoved" : 2 })
> db.bb.find().pretty()
{
"_id" : ObjectId("5ed6554230571733ccb3d67b"),
"name" : "jay",
"hello" : "ball"
}
> db.aa.find().pretty()
{ "_id" : ObjectId("5ed7b329582b5fea09b920a6"), "name" : "aa", "id" : 100 }
{ "_id" : ObjectId("5ed7b351582b5fea09b920a7"), "name" : "bb", "id" : 101 }
> db.aa.remove()
2020-06-03T10:30:01.574-0400 E QUERY [js] uncaught exception: Error: remove needs a query :
DBCollection.prototype._parseRemove@src/mongo/shell/collection.js:357:15
DBCollection.prototype.remove@src/mongo/shell/collection.js:384:18
@(shell):1:1
> db.aa.remove({})
WriteResult({ "nRemoved" : 2 })
> db.aa.find().pretty()
> show collections
aa
注意,remove()删除所有文档时需要添加查询语句,也就是{},否则会报错。另外,remove()不会删除集合本身。
如果需要连集合一起删除的话,可以使用drop()
> db.aa.find().pretty()
{ "_id" : ObjectId("5ed7b4f5582b5fea09b920ac"), "name" : "aa", "id" : 100 }
{ "_id" : ObjectId("5ed7b4f8582b5fea09b920ad"), "name" : "bb", "id" : 101 }
> db.aa.drop()
true
> show collections
>
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db.fsyncLock()
{
"info" : "now locked against writes, use db.fsyncUnlock() to unlock",
"lockCount" : NumberLong(1),
"seeAlso" : "http://dochub.mongodb.org/core/fsynccommand",
"ok" : 1
}
备份完之后,再使用以下命令解锁,
> db.fsyncUnlock()
{ "info" : "fsyncUnlock completed", "lockCount" : NumberLong(0), "ok" : 1 }
mongodump -d dbname -u username -p password --authenticationDatabase admin --host="mongodb-ip:27017" -o /home/data/mydb_$(date +%Y%m%d)
其中,
-d指定数据库名称
-u指定用户名
-p指定密码
–authenticationDatabase指定要连接认证的数据库
–host指定数据库ip及端口号
-o指定备份文件存放路径
执行后就能到对应目录查看备份信息,
[root@localhost data]# mongodump -d abc -u root -p root --authenticationDatabase admin --host="localhost:27017" -o /home/mongodb/data/db_$(date +%Y%m%d)
2020-06-20T23:32:59.930-0400 writing abc.bb to
2020-06-20T23:32:59.931-0400 done dumping abc.bb (2 documents)
[root@localhost data]# ls
db_20200620
[root@localhost data]# cd db_20200620/
[root@localhost db_20200620]# ls
abc
[root@localhost db_20200620]# cd abc/
[root@localhost abc]# ls
bb.bson bb.metadata.json
对于dump出来的文件,我们可以用bsondump文件查看,
[root@localhost abc]# bsondump bb.bson
{"_id":{"$oid":"5ed6554230571733ccb3d67b"},"name":"jay","hello":"ball","animals":{"dog":{"$numberDouble":"50.0"},"cat":{"$numberDouble":"100.0"}},"happy":"ending","players":[{"name":"aa","score":{"$numberDouble":"98.0"}},{"name":"dan","score":{"$numberDouble":"95.0"}},{"name":"du","score":{"$numberDouble":"100.0"}}]}
{"_id":{"$oid":"5ed6579b30571733ccb3d67c"},"name":"jack","hello":"may","happy":"ending","players":[{"name":"aa","score":{"$numberDouble":"99.0"}},{"name":"dan","score":{"$numberDouble":"98.0"}},{"name":"du","score":{"$numberDouble":"90.0"}}]}
2020-06-20T23:44:27.676-0400 2 objects found
备份时还会涉及到一个问题,备份需要时间,因此需要考虑备份时的数据修改如何处理,一不小心就会造成数据不一致的情况,这时就可以使用< --oplog>参数,但会保存备份开始到结束时这段时间数据库的所有操作,然后在恢复备份时将该操作回放,以保证数据的一致性。
备份后,我们把该数据库的集合删除,模拟异常,
> db
abc
> show collections
bb
> db.bb.drop()
true
>
然后用刚才备份的数据进行恢复,使用mongorestore命令,
[root@localhost data]# mongorestore --host=localhost:27017 -u root --authenticationDatabase=admin /home/mongodb/data/db_20200620
Enter password:
2020-06-20T23:56:38.747-0400 preparing collections to restore from
2020-06-20T23:56:38.747-0400 reading metadata for abc.bb from /home/mongodb/data/db_20200620/abc/bb.metadata.json
2020-06-20T23:56:38.754-0400 restoring abc.bb from /home/mongodb/data/db_20200620/abc/bb.bson
2020-06-20T23:56:38.757-0400 no indexes to restore
2020-06-20T23:56:38.757-0400 finished restoring abc.bb (2 documents, 0 failures)
2020-06-20T23:56:38.757-0400 2 document(s) restored successfully. 0 document(s) failed to restore.
恢复完成后,在数据库中查看数据,确认完整性,
> show collections
bb
> db.bb.find()
{ "_id" : ObjectId("5ed6554230571733ccb3d67b"), "name" : "jay", "hello" : "ball", "animals" : { "dog" : 50, "cat" : 100 }, "happy" : "ending", "players" : [ { "name" : "aa", "score" : 98 }, { "name" : "dan", "score" : 95 }, { "name" : "du", "score" : 100 } ] }
{ "_id" : ObjectId("5ed6579b30571733ccb3d67c"), "name" : "jack", "hello" : "may", "happy" : "ending", "players" : [ { "name" : "aa", "score" : 99 }, { "name" : "dan", "score" : 98 }, { "name" : "du", "score" : 90 } ] }
>
当然,如果备份时使用了< --oplog>参数,恢复时需要使用< --oplogReplay>参数,回放操作。