赞
踩
目录
导出数据命令:mongoexport -h dbhost -d dbname -c collectionName -o output
-h :数据库地址,MongoDB 服务器所在的 IP 与 端口,如 localhost:27017 -d :指明使用的数据库实例,如 test -c 指明要导出的集合,如 c1 -o 指明要导出的文件名,如 E:/wmx/mongoDump/c1.json,注意是文件而不是目录,目录不存在时会一同新建 |
与 《 MongoDB 数据备份 与 恢复》同理,它同样使用安装的 MongoDB 目录下的 bin 目录下的 mongoexport.exe 与 mongoimport.exe
- C:\Users\Administrator.SC-201707281232>mongoexport --help
- Usage:
- mongoexport <options>
-
- Export data from MongoDB in CSV or JSON format.
-
- See http://docs.mongodb.org/manual/reference/program/mongoexport/ for more information.
-
- general options:
- /help print usage
- /version print the tool version and
- exit
-
- verbosity options:
- /v, /verbose:<level> more detailed log output
- .......
操作时,同样不用登陆 MongoDB,在 cmd 命令行中直接操作即可,如下所示,mongoDB 一共 5个 有数据的 库,以 mydb1 数据库实例中的 c1 集合为例进行导出。
- > show dbs
- admin 0.000GB
- config 0.000GB
- local 0.000GB
- mydb1 0.000GB
- mydb2 0.186GB
- > db
- mydb1
- > show collections
- c1
- > db.c1.find().count()
- 100
- > db.c1.find()
- { "_id" : ObjectId("5b98bc379253fbe383c9f04e"), "name" : "zhangSan1", "age" : 1 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f04f"), "name" : "zhangSan2", "age" : 2 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f050"), "name" : "zhangSan3", "age" : 3 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f051"), "name" : "zhangSan4", "age" : 4 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f052"), "name" : "zhangSan5", "age" : 5 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f053"), "name" : "zhangSan6", "age" : 6 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f054"), "name" : "zhangSan7", "age" : 7 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f055"), "name" : "zhangSan8", "age" : 8 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f056"), "name" : "zhangSan9", "age" : 9 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f057"), "name" : "zhangSan10", "age" : 10 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f058"), "name" : "zhangSan11", "age" : 11 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f059"), "name" : "zhangSan12", "age" : 12 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f05a"), "name" : "zhangSan13", "age" : 13 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f05b"), "name" : "zhangSan14", "age" : 14 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f05c"), "name" : "zhangSan15", "age" : 15 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f05d"), "name" : "zhangSan16", "age" : 16 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f05e"), "name" : "zhangSan17", "age" : 17 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f05f"), "name" : "zhangSan18", "age" : 18 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f060"), "name" : "zhangSan19", "age" : 19 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f061"), "name" : "zhangSan20", "age" : 20 }
- Type "it" for more
- >
如下所示,mongoexport -h localhost:27017 -d mydb1 -c c1 -o E:/wmx/mongoDump/c1.json 将 数据库 mydb1 下的 集合 c1 导出到 E:/wmx/mongoDump/c1.json 文件中,存储文件可以是多种形式,如 txt、xls、docs 等等
- C:\Users\Administrator.SC-201707281232>mongoexport -h localhost:27017 -d mydb1 -c c1 -o E:/wmx/mongoDump/c1.json
- 2018-09-12T16:42:07.297+0800 connected to: localhost:27017
- 2018-09-12T16:42:07.379+0800 exported 100 records
-
- C:\Users\Administrator.SC-201707281232>mongoexport -h localhost:27017 -d mydb1 -c c1 -o E:/wmx/mongoDump/c1.txt
- 2018-09-12T16:42:58.225+0800 connected to: localhost:27017
- 2018-09-12T16:42:58.311+0800 exported 100 records
-
- C:\Users\Administrator.SC-201707281232>
如下所示,导出数据成功。
导入数据命令:mongoimport -h dbhost -d dbname -c collectionname 文件的地址...
-h : 数据库地址,MongoDB 服务器所在的 IP 与 端口,如 localhost:27017 -d :指明使用的库,指明使用的数据库实例,如 test -c :指明要导入的集合,如 c1、c2、可以和导出时不一致,自定义即可,不存在时会直接创建。 本地的文件地址:事先导出好的 mongoDB 集合文件 |
如下所示,先删除 mydb1 库下面的 集合 c1,然后再将本地之前导出好的进行导入恢复
- > db
- mydb1
- > show tables
- c1
- > db.c1.drop()
- true
- > show tables
- >
如下所示,直接从 cmd 命令行中进行操作,不用登录 MongoDB,将上面备份好的 c1.txt 与 c1.json 文件进行导入,分别导入到数据库 mydb1 下面的 c1 集合 与 c2 集合,c1、c2 集合事先是不存在的。
- C:\Users\Administrator.SC-201707281232>mongoimport -h localhost:27017 -d mydb1 -c c2 E:/wmx/mongoDump/c1.txt
- 2018-09-12T16:56:21.426+0800 connected to: localhost:27017
- 2018-09-12T16:56:21.752+0800 imported 100 documents
-
- C:\Users\Administrator.SC-201707281232>mongoimport -h localhost:27017 -d mydb1 -c c1 E:/wmx/mongoDump/c1.json
- 2018-09-12T16:57:08.308+0800 connected to: localhost:27017
- 2018-09-12T16:57:08.653+0800 imported 100 documents
-
- C:\Users\Administrator.SC-201707281232>
然后登录 MongoDB 再次查询时,数据导入成功
- > show tables
- c1
- c2
- > db.c1.find().count()
- 100
- > db.c2.find().count()
- 100
- > db.c1.find()
- { "_id" : ObjectId("5b98bc379253fbe383c9f051"), "name" : "zhangSan4", "age" : 4 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f04f"), "name" : "zhangSan2", "age" : 2 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f04e"), "name" : "zhangSan1", "age" : 1 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f050"), "name" : "zhangSan3", "age" : 3 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f053"), "name" : "zhangSan6", "age" : 6 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f052"), "name" : "zhangSan5", "age" : 5 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f055"), "name" : "zhangSan8", "age" : 8 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f054"), "name" : "zhangSan7", "age" : 7 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f056"), "name" : "zhangSan9", "age" : 9 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f057"), "name" : "zhangSan10", "age" : 10 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f058"), "name" : "zhangSan11", "age" : 11 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f05b"), "name" : "zhangSan14", "age" : 14 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f05a"), "name" : "zhangSan13", "age" : 13 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f05c"), "name" : "zhangSan15", "age" : 15 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f05d"), "name" : "zhangSan16", "age" : 16 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f05e"), "name" : "zhangSan17", "age" : 17 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f05f"), "name" : "zhangSan18", "age" : 18 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f059"), "name" : "zhangSan12", "age" : 12 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f063"), "name" : "zhangSan22", "age" : 22 }
- { "_id" : ObjectId("5b98bc379253fbe383c9f060"), "name" : "zhangSan19", "age" : 19 }
- Type "it" for more
固定集合指的是事先创建而且大小固定的集合 。
固定集合特性:固定集合很像环形队列,如果空间不足,最早的文档就会被删除,为新的文档腾出空间。一般来说,固定集合适用于任何想要自动淘汰过期属性的场景,没有太多的操作限制。
固定集合使用场景:比如通话记录,聊天记录 等,只保存一定大小的历史记录,过期的则会进行删除!
固定集合创建命令:db.createCollection(“collectionName”,{capped:true,size:100000,max:100}); size 指定集合存储大小,单位为 KB,max 指定文档的数量
当指定文档数量上限时,必须同时指定大小。淘汰机制只有在容量还没有满时才会依据文档数量来工作。要是容量满了,淘汰机制会依据容量来工作。
固定集合创建命令:db.createCollection(“collectionName”,{capped:true,size:100000,max:100});
capped:是否固定、size:指定集合大小,单位为KB、max 指定文档的数量
固定集合创建命令其实也就是在显示创建集合命令基础上带了后面的 花括号参数而已,可以参考《 MongoDB 数据库_集合_文档 操作》
如下所示 db.createCollection("capped1",{capped:true,size:1024}):当只指定了固定集合的存储大小时,只根据大小进行淘汰,当集合大小超过 1024 KB 时,淘汰旧值,添加新值——推陈出新!
如下所示当连续添加 100 条时,因为集合大小超过了 1M,所以前面添加的旧文档被删除了,只留下了最新的 18 条。
- > db
- mydb2
- > show collections
- c1
- > db.createCollection("capped1",{capped:true,size:1024})
- { "ok" : 1 }
- > for(var i=0;i<=100;i++){
- ... db.capped1.insert({name:"zhangSan",age:i});
- ... }
- WriteResult({ "nInserted" : 1 })
- > db.capped1.find().count()
- 18
- > db.capped1.find()
- { "_id" : ObjectId("5b98a3d4496484420f387dab"), "name" : "zhangSan", "age" : 83 }
- { "_id" : ObjectId("5b98a3d4496484420f387dac"), "name" : "zhangSan", "age" : 84 }
- { "_id" : ObjectId("5b98a3d4496484420f387dad"), "name" : "zhangSan", "age" : 85 }
- { "_id" : ObjectId("5b98a3d4496484420f387dae"), "name" : "zhangSan", "age" : 86 }
- { "_id" : ObjectId("5b98a3d4496484420f387daf"), "name" : "zhangSan", "age" : 87 }
- { "_id" : ObjectId("5b98a3d4496484420f387db0"), "name" : "zhangSan", "age" : 88 }
- { "_id" : ObjectId("5b98a3d4496484420f387db1"), "name" : "zhangSan", "age" : 89 }
- { "_id" : ObjectId("5b98a3d4496484420f387db2"), "name" : "zhangSan", "age" : 90 }
- { "_id" : ObjectId("5b98a3d4496484420f387db3"), "name" : "zhangSan", "age" : 91 }
- { "_id" : ObjectId("5b98a3d4496484420f387db4"), "name" : "zhangSan", "age" : 92 }
- { "_id" : ObjectId("5b98a3d4496484420f387db5"), "name" : "zhangSan", "age" : 93 }
- { "_id" : ObjectId("5b98a3d4496484420f387db6"), "name" : "zhangSan", "age" : 94 }
- { "_id" : ObjectId("5b98a3d4496484420f387db7"), "name" : "zhangSan", "age" : 95 }
- { "_id" : ObjectId("5b98a3d4496484420f387db8"), "name" : "zhangSan", "age" : 96 }
- { "_id" : ObjectId("5b98a3d4496484420f387db9"), "name" : "zhangSan", "age" : 97 }
- { "_id" : ObjectId("5b98a3d4496484420f387dba"), "name" : "zhangSan", "age" : 98 }
- { "_id" : ObjectId("5b98a3d4496484420f387dbb"), "name" : "zhangSan", "age" : 99 }
- { "_id" : ObjectId("5b98a3d4496484420f387dbc"), "name" : "zhangSan", "age" : 100 }
- >
当指定文档数量上限时,必须同时指定集合存储大小。
淘汰机制是:集合大小未超过时,则根据文档个数进行约束;集合大小超过时,则根据集合大小进行约束
如下所示 db.createCollection("capped2",{capped:true,size:10240,max:100}):创建固定集合 capped2,其集合存储大小为 10 M,文档个数上限为 100 个
当一次性添加 100 个文档之后,因为 集合存储大小未超过 10 M,所以 100 个文档全部添加成功,当之后再次添加时文档时,因为超过 100 个文档个数的上限,所以第一条旧文档开始被删除,以此类推。
- > db.createCollection("capped2",{capped:true,size:10240,max:100})
- { "ok" : 1 }
- > for(var i=1;i<=100;i++){
- ... db.capped2.insert({name:"liSi",age:i});
- ... }
- WriteResult({ "nInserted" : 1 })
- > db.capped2.find().count()
- 100
- > db.capped2.find()
- { "_id" : ObjectId("5b98a707496484420f387dbd"), "name" : "liSi", "age" : 1 }
- { "_id" : ObjectId("5b98a707496484420f387dbe"), "name" : "liSi", "age" : 2 }
- { "_id" : ObjectId("5b98a707496484420f387dbf"), "name" : "liSi", "age" : 3 }
- { "_id" : ObjectId("5b98a707496484420f387dc0"), "name" : "liSi", "age" : 4 }
- { "_id" : ObjectId("5b98a707496484420f387dc1"), "name" : "liSi", "age" : 5 }
- { "_id" : ObjectId("5b98a707496484420f387dc2"), "name" : "liSi", "age" : 6 }
- { "_id" : ObjectId("5b98a707496484420f387dc3"), "name" : "liSi", "age" : 7 }
- { "_id" : ObjectId("5b98a707496484420f387dc4"), "name" : "liSi", "age" : 8 }
- { "_id" : ObjectId("5b98a707496484420f387dc5"), "name" : "liSi", "age" : 9 }
- { "_id" : ObjectId("5b98a707496484420f387dc6"), "name" : "liSi", "age" : 10 }
- { "_id" : ObjectId("5b98a707496484420f387dc7"), "name" : "liSi", "age" : 11 }
- { "_id" : ObjectId("5b98a707496484420f387dc8"), "name" : "liSi", "age" : 12 }
- { "_id" : ObjectId("5b98a707496484420f387dc9"), "name" : "liSi", "age" : 13 }
- { "_id" : ObjectId("5b98a707496484420f387dca"), "name" : "liSi", "age" : 14 }
- { "_id" : ObjectId("5b98a707496484420f387dcb"), "name" : "liSi", "age" : 15 }
- { "_id" : ObjectId("5b98a707496484420f387dcc"), "name" : "liSi", "age" : 16 }
- { "_id" : ObjectId("5b98a707496484420f387dcd"), "name" : "liSi", "age" : 17 }
- { "_id" : ObjectId("5b98a707496484420f387dce"), "name" : "liSi", "age" : 18 }
- { "_id" : ObjectId("5b98a707496484420f387dcf"), "name" : "liSi", "age" : 19 }
- { "_id" : ObjectId("5b98a707496484420f387dd0"), "name" : "liSi", "age" : 20 }
- Type "it" for more
- > db.capped2.insert({name:"wangWu",age:25})
- WriteResult({ "nInserted" : 1 })
- > db.capped2.find().count()
- 100
- > db.capped2.find()
- { "_id" : ObjectId("5b98a707496484420f387dbe"), "name" : "liSi", "age" : 2 }
- { "_id" : ObjectId("5b98a707496484420f387dbf"), "name" : "liSi", "age" : 3 }
- { "_id" : ObjectId("5b98a707496484420f387dc0"), "name" : "liSi", "age" : 4 }
- { "_id" : ObjectId("5b98a707496484420f387dc1"), "name" : "liSi", "age" : 5 }
- { "_id" : ObjectId("5b98a707496484420f387dc2"), "name" : "liSi", "age" : 6 }
- { "_id" : ObjectId("5b98a707496484420f387dc3"), "name" : "liSi", "age" : 7 }
- { "_id" : ObjectId("5b98a707496484420f387dc4"), "name" : "liSi", "age" : 8 }
- { "_id" : ObjectId("5b98a707496484420f387dc5"), "name" : "liSi", "age" : 9 }
- { "_id" : ObjectId("5b98a707496484420f387dc6"), "name" : "liSi", "age" : 10 }
- { "_id" : ObjectId("5b98a707496484420f387dc7"), "name" : "liSi", "age" : 11 }
- { "_id" : ObjectId("5b98a707496484420f387dc8"), "name" : "liSi", "age" : 12 }
- { "_id" : ObjectId("5b98a707496484420f387dc9"), "name" : "liSi", "age" : 13 }
- { "_id" : ObjectId("5b98a707496484420f387dca"), "name" : "liSi", "age" : 14 }
- { "_id" : ObjectId("5b98a707496484420f387dcb"), "name" : "liSi", "age" : 15 }
- { "_id" : ObjectId("5b98a707496484420f387dcc"), "name" : "liSi", "age" : 16 }
- { "_id" : ObjectId("5b98a707496484420f387dcd"), "name" : "liSi", "age" : 17 }
- { "_id" : ObjectId("5b98a707496484420f387dce"), "name" : "liSi", "age" : 18 }
- { "_id" : ObjectId("5b98a707496484420f387dcf"), "name" : "liSi", "age" : 19 }
- { "_id" : ObjectId("5b98a707496484420f387dd0"), "name" : "liSi", "age" : 20 }
- { "_id" : ObjectId("5b98a707496484420f387dd1"), "name" : "liSi", "age" : 21 }
- Type "it" for more
- >
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。