当前位置:   article > 正文

MongoDB 命令行 操作固定集合,数据集合导出 与 导入_mongodb 数据库不显示,如何导入集合

mongodb 数据库不显示,如何导入集合

目录

数据集合导出 与 导入

导出(mongoexport) 

导入(mongoimport)

固定集合( capped collection)

固定集合操作

集合大小单独约束

大小与数理同时约束


数据集合导出 与 导入

导出(mongoexport) 

导出数据命令: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 

  • 如果想要查看所有的参数信息,可以使用 mongoexport --help 进行查看
  1. C:\Users\Administrator.SC-201707281232>mongoexport --help
  2. Usage:
  3. mongoexport <options>
  4. Export data from MongoDB in CSV or JSON format.
  5. See http://docs.mongodb.org/manual/reference/program/mongoexport/ for more information.
  6. general options:
  7. /help print usage
  8. /version print the tool version and
  9. exit
  10. verbosity options:
  11. /v, /verbose:<level> more detailed log output
  12. .......

操作时,同样不用登陆 MongoDB,在 cmd 命令行中直接操作即可,如下所示,mongoDB 一共 5个 有数据的 库,以 mydb1 数据库实例中的 c1 集合为例进行导出。

  1. > show dbs
  2. admin 0.000GB
  3. config 0.000GB
  4. local 0.000GB
  5. mydb1 0.000GB
  6. mydb2 0.186GB
  7. > db
  8. mydb1
  9. > show collections
  10. c1
  11. > db.c1.find().count()
  12. 100
  13. > db.c1.find()
  14. { "_id" : ObjectId("5b98bc379253fbe383c9f04e"), "name" : "zhangSan1", "age" : 1 }
  15. { "_id" : ObjectId("5b98bc379253fbe383c9f04f"), "name" : "zhangSan2", "age" : 2 }
  16. { "_id" : ObjectId("5b98bc379253fbe383c9f050"), "name" : "zhangSan3", "age" : 3 }
  17. { "_id" : ObjectId("5b98bc379253fbe383c9f051"), "name" : "zhangSan4", "age" : 4 }
  18. { "_id" : ObjectId("5b98bc379253fbe383c9f052"), "name" : "zhangSan5", "age" : 5 }
  19. { "_id" : ObjectId("5b98bc379253fbe383c9f053"), "name" : "zhangSan6", "age" : 6 }
  20. { "_id" : ObjectId("5b98bc379253fbe383c9f054"), "name" : "zhangSan7", "age" : 7 }
  21. { "_id" : ObjectId("5b98bc379253fbe383c9f055"), "name" : "zhangSan8", "age" : 8 }
  22. { "_id" : ObjectId("5b98bc379253fbe383c9f056"), "name" : "zhangSan9", "age" : 9 }
  23. { "_id" : ObjectId("5b98bc379253fbe383c9f057"), "name" : "zhangSan10", "age" : 10 }
  24. { "_id" : ObjectId("5b98bc379253fbe383c9f058"), "name" : "zhangSan11", "age" : 11 }
  25. { "_id" : ObjectId("5b98bc379253fbe383c9f059"), "name" : "zhangSan12", "age" : 12 }
  26. { "_id" : ObjectId("5b98bc379253fbe383c9f05a"), "name" : "zhangSan13", "age" : 13 }
  27. { "_id" : ObjectId("5b98bc379253fbe383c9f05b"), "name" : "zhangSan14", "age" : 14 }
  28. { "_id" : ObjectId("5b98bc379253fbe383c9f05c"), "name" : "zhangSan15", "age" : 15 }
  29. { "_id" : ObjectId("5b98bc379253fbe383c9f05d"), "name" : "zhangSan16", "age" : 16 }
  30. { "_id" : ObjectId("5b98bc379253fbe383c9f05e"), "name" : "zhangSan17", "age" : 17 }
  31. { "_id" : ObjectId("5b98bc379253fbe383c9f05f"), "name" : "zhangSan18", "age" : 18 }
  32. { "_id" : ObjectId("5b98bc379253fbe383c9f060"), "name" : "zhangSan19", "age" : 19 }
  33. { "_id" : ObjectId("5b98bc379253fbe383c9f061"), "name" : "zhangSan20", "age" : 20 }
  34. Type "it" for more
  35. >

如下所示,mongoexport -h localhost:27017 -d mydb1 -c c1 -o E:/wmx/mongoDump/c1.json 将 数据库 mydb1 下的 集合 c1 导出到 E:/wmx/mongoDump/c1.json 文件中,存储文件可以是多种形式,如 txt、xls、docs 等等

  1. C:\Users\Administrator.SC-201707281232>mongoexport -h localhost:27017 -d mydb1 -c c1 -o E:/wmx/mongoDump/c1.json
  2. 2018-09-12T16:42:07.297+0800 connected to: localhost:27017
  3. 2018-09-12T16:42:07.379+0800 exported 100 records
  4. C:\Users\Administrator.SC-201707281232>mongoexport -h localhost:27017 -d mydb1 -c c1 -o E:/wmx/mongoDump/c1.txt
  5. 2018-09-12T16:42:58.225+0800 connected to: localhost:27017
  6. 2018-09-12T16:42:58.311+0800 exported 100 records
  7. C:\Users\Administrator.SC-201707281232>

如下所示,导出数据成功。

导入(mongoimport)

导入数据命令:mongoimport -h dbhost -d dbname -c collectionname 文件的地址...

-h : 数据库地址,MongoDB 服务器所在的 IP 与 端口,如 localhost:27017

-d :指明使用的库,指明使用的数据库实例,如 test

-c :指明要导入的集合,如 c1、c2、可以和导出时不一致,自定义即可,不存在时会直接创建。

本地的文件地址:事先导出好的 mongoDB 集合文件

如下所示,先删除 mydb1 库下面的 集合 c1,然后再将本地之前导出好的进行导入恢复

  1. > db
  2. mydb1
  3. > show tables
  4. c1
  5. > db.c1.drop()
  6. true
  7. > show tables
  8. >

如下所示,直接从 cmd 命令行中进行操作,不用登录 MongoDB,将上面备份好的 c1.txt 与 c1.json 文件进行导入,分别导入到数据库 mydb1 下面的 c1 集合 与 c2 集合,c1、c2 集合事先是不存在的。

  1. C:\Users\Administrator.SC-201707281232>mongoimport -h localhost:27017 -d mydb1 -c c2 E:/wmx/mongoDump/c1.txt
  2. 2018-09-12T16:56:21.426+0800 connected to: localhost:27017
  3. 2018-09-12T16:56:21.752+0800 imported 100 documents
  4. C:\Users\Administrator.SC-201707281232>mongoimport -h localhost:27017 -d mydb1 -c c1 E:/wmx/mongoDump/c1.json
  5. 2018-09-12T16:57:08.308+0800 connected to: localhost:27017
  6. 2018-09-12T16:57:08.653+0800 imported 100 documents
  7. C:\Users\Administrator.SC-201707281232>

然后登录 MongoDB 再次查询时,数据导入成功

  1. > show tables
  2. c1
  3. c2
  4. > db.c1.find().count()
  5. 100
  6. > db.c2.find().count()
  7. 100
  8. > db.c1.find()
  9. { "_id" : ObjectId("5b98bc379253fbe383c9f051"), "name" : "zhangSan4", "age" : 4 }
  10. { "_id" : ObjectId("5b98bc379253fbe383c9f04f"), "name" : "zhangSan2", "age" : 2 }
  11. { "_id" : ObjectId("5b98bc379253fbe383c9f04e"), "name" : "zhangSan1", "age" : 1 }
  12. { "_id" : ObjectId("5b98bc379253fbe383c9f050"), "name" : "zhangSan3", "age" : 3 }
  13. { "_id" : ObjectId("5b98bc379253fbe383c9f053"), "name" : "zhangSan6", "age" : 6 }
  14. { "_id" : ObjectId("5b98bc379253fbe383c9f052"), "name" : "zhangSan5", "age" : 5 }
  15. { "_id" : ObjectId("5b98bc379253fbe383c9f055"), "name" : "zhangSan8", "age" : 8 }
  16. { "_id" : ObjectId("5b98bc379253fbe383c9f054"), "name" : "zhangSan7", "age" : 7 }
  17. { "_id" : ObjectId("5b98bc379253fbe383c9f056"), "name" : "zhangSan9", "age" : 9 }
  18. { "_id" : ObjectId("5b98bc379253fbe383c9f057"), "name" : "zhangSan10", "age" : 10 }
  19. { "_id" : ObjectId("5b98bc379253fbe383c9f058"), "name" : "zhangSan11", "age" : 11 }
  20. { "_id" : ObjectId("5b98bc379253fbe383c9f05b"), "name" : "zhangSan14", "age" : 14 }
  21. { "_id" : ObjectId("5b98bc379253fbe383c9f05a"), "name" : "zhangSan13", "age" : 13 }
  22. { "_id" : ObjectId("5b98bc379253fbe383c9f05c"), "name" : "zhangSan15", "age" : 15 }
  23. { "_id" : ObjectId("5b98bc379253fbe383c9f05d"), "name" : "zhangSan16", "age" : 16 }
  24. { "_id" : ObjectId("5b98bc379253fbe383c9f05e"), "name" : "zhangSan17", "age" : 17 }
  25. { "_id" : ObjectId("5b98bc379253fbe383c9f05f"), "name" : "zhangSan18", "age" : 18 }
  26. { "_id" : ObjectId("5b98bc379253fbe383c9f059"), "name" : "zhangSan12", "age" : 12 }
  27. { "_id" : ObjectId("5b98bc379253fbe383c9f063"), "name" : "zhangSan22", "age" : 22 }
  28. { "_id" : ObjectId("5b98bc379253fbe383c9f060"), "name" : "zhangSan19", "age" : 19 }
  29. Type "it" for more

固定集合( capped collection)

固定集合指的是事先创建而且大小固定的集合 。

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

固定集合使用场景:比如通话记录,聊天记录 等,只保存一定大小的历史记录,过期的则会进行删除!

固定集合创建命令: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 条。

  1. > db
  2. mydb2
  3. > show collections
  4. c1
  5. > db.createCollection("capped1",{capped:true,size:1024})
  6. { "ok" : 1 }
  7. > for(var i=0;i<=100;i++){
  8. ... db.capped1.insert({name:"zhangSan",age:i});
  9. ... }
  10. WriteResult({ "nInserted" : 1 })
  11. > db.capped1.find().count()
  12. 18
  13. > db.capped1.find()
  14. { "_id" : ObjectId("5b98a3d4496484420f387dab"), "name" : "zhangSan", "age" : 83 }
  15. { "_id" : ObjectId("5b98a3d4496484420f387dac"), "name" : "zhangSan", "age" : 84 }
  16. { "_id" : ObjectId("5b98a3d4496484420f387dad"), "name" : "zhangSan", "age" : 85 }
  17. { "_id" : ObjectId("5b98a3d4496484420f387dae"), "name" : "zhangSan", "age" : 86 }
  18. { "_id" : ObjectId("5b98a3d4496484420f387daf"), "name" : "zhangSan", "age" : 87 }
  19. { "_id" : ObjectId("5b98a3d4496484420f387db0"), "name" : "zhangSan", "age" : 88 }
  20. { "_id" : ObjectId("5b98a3d4496484420f387db1"), "name" : "zhangSan", "age" : 89 }
  21. { "_id" : ObjectId("5b98a3d4496484420f387db2"), "name" : "zhangSan", "age" : 90 }
  22. { "_id" : ObjectId("5b98a3d4496484420f387db3"), "name" : "zhangSan", "age" : 91 }
  23. { "_id" : ObjectId("5b98a3d4496484420f387db4"), "name" : "zhangSan", "age" : 92 }
  24. { "_id" : ObjectId("5b98a3d4496484420f387db5"), "name" : "zhangSan", "age" : 93 }
  25. { "_id" : ObjectId("5b98a3d4496484420f387db6"), "name" : "zhangSan", "age" : 94 }
  26. { "_id" : ObjectId("5b98a3d4496484420f387db7"), "name" : "zhangSan", "age" : 95 }
  27. { "_id" : ObjectId("5b98a3d4496484420f387db8"), "name" : "zhangSan", "age" : 96 }
  28. { "_id" : ObjectId("5b98a3d4496484420f387db9"), "name" : "zhangSan", "age" : 97 }
  29. { "_id" : ObjectId("5b98a3d4496484420f387dba"), "name" : "zhangSan", "age" : 98 }
  30. { "_id" : ObjectId("5b98a3d4496484420f387dbb"), "name" : "zhangSan", "age" : 99 }
  31. { "_id" : ObjectId("5b98a3d4496484420f387dbc"), "name" : "zhangSan", "age" : 100 }
  32. >

大小与数理同时约束

当指定文档数量上限时,必须同时指定集合存储大小。

淘汰机制是:集合大小未超过时,则根据文档个数进行约束;集合大小超过时,则根据集合大小进行约束

如下所示 db.createCollection("capped2",{capped:true,size:10240,max:100}):创建固定集合 capped2,其集合存储大小为 10 M,文档个数上限为 100 个

当一次性添加 100 个文档之后,因为 集合存储大小未超过 10 M,所以 100 个文档全部添加成功,当之后再次添加时文档时,因为超过 100 个文档个数的上限,所以第一条旧文档开始被删除,以此类推。

  1. > db.createCollection("capped2",{capped:true,size:10240,max:100})
  2. { "ok" : 1 }
  3. > for(var i=1;i<=100;i++){
  4. ... db.capped2.insert({name:"liSi",age:i});
  5. ... }
  6. WriteResult({ "nInserted" : 1 })
  7. > db.capped2.find().count()
  8. 100
  9. > db.capped2.find()
  10. { "_id" : ObjectId("5b98a707496484420f387dbd"), "name" : "liSi", "age" : 1 }
  11. { "_id" : ObjectId("5b98a707496484420f387dbe"), "name" : "liSi", "age" : 2 }
  12. { "_id" : ObjectId("5b98a707496484420f387dbf"), "name" : "liSi", "age" : 3 }
  13. { "_id" : ObjectId("5b98a707496484420f387dc0"), "name" : "liSi", "age" : 4 }
  14. { "_id" : ObjectId("5b98a707496484420f387dc1"), "name" : "liSi", "age" : 5 }
  15. { "_id" : ObjectId("5b98a707496484420f387dc2"), "name" : "liSi", "age" : 6 }
  16. { "_id" : ObjectId("5b98a707496484420f387dc3"), "name" : "liSi", "age" : 7 }
  17. { "_id" : ObjectId("5b98a707496484420f387dc4"), "name" : "liSi", "age" : 8 }
  18. { "_id" : ObjectId("5b98a707496484420f387dc5"), "name" : "liSi", "age" : 9 }
  19. { "_id" : ObjectId("5b98a707496484420f387dc6"), "name" : "liSi", "age" : 10 }
  20. { "_id" : ObjectId("5b98a707496484420f387dc7"), "name" : "liSi", "age" : 11 }
  21. { "_id" : ObjectId("5b98a707496484420f387dc8"), "name" : "liSi", "age" : 12 }
  22. { "_id" : ObjectId("5b98a707496484420f387dc9"), "name" : "liSi", "age" : 13 }
  23. { "_id" : ObjectId("5b98a707496484420f387dca"), "name" : "liSi", "age" : 14 }
  24. { "_id" : ObjectId("5b98a707496484420f387dcb"), "name" : "liSi", "age" : 15 }
  25. { "_id" : ObjectId("5b98a707496484420f387dcc"), "name" : "liSi", "age" : 16 }
  26. { "_id" : ObjectId("5b98a707496484420f387dcd"), "name" : "liSi", "age" : 17 }
  27. { "_id" : ObjectId("5b98a707496484420f387dce"), "name" : "liSi", "age" : 18 }
  28. { "_id" : ObjectId("5b98a707496484420f387dcf"), "name" : "liSi", "age" : 19 }
  29. { "_id" : ObjectId("5b98a707496484420f387dd0"), "name" : "liSi", "age" : 20 }
  30. Type "it" for more
  31. > db.capped2.insert({name:"wangWu",age:25})
  32. WriteResult({ "nInserted" : 1 })
  33. > db.capped2.find().count()
  34. 100
  35. > db.capped2.find()
  36. { "_id" : ObjectId("5b98a707496484420f387dbe"), "name" : "liSi", "age" : 2 }
  37. { "_id" : ObjectId("5b98a707496484420f387dbf"), "name" : "liSi", "age" : 3 }
  38. { "_id" : ObjectId("5b98a707496484420f387dc0"), "name" : "liSi", "age" : 4 }
  39. { "_id" : ObjectId("5b98a707496484420f387dc1"), "name" : "liSi", "age" : 5 }
  40. { "_id" : ObjectId("5b98a707496484420f387dc2"), "name" : "liSi", "age" : 6 }
  41. { "_id" : ObjectId("5b98a707496484420f387dc3"), "name" : "liSi", "age" : 7 }
  42. { "_id" : ObjectId("5b98a707496484420f387dc4"), "name" : "liSi", "age" : 8 }
  43. { "_id" : ObjectId("5b98a707496484420f387dc5"), "name" : "liSi", "age" : 9 }
  44. { "_id" : ObjectId("5b98a707496484420f387dc6"), "name" : "liSi", "age" : 10 }
  45. { "_id" : ObjectId("5b98a707496484420f387dc7"), "name" : "liSi", "age" : 11 }
  46. { "_id" : ObjectId("5b98a707496484420f387dc8"), "name" : "liSi", "age" : 12 }
  47. { "_id" : ObjectId("5b98a707496484420f387dc9"), "name" : "liSi", "age" : 13 }
  48. { "_id" : ObjectId("5b98a707496484420f387dca"), "name" : "liSi", "age" : 14 }
  49. { "_id" : ObjectId("5b98a707496484420f387dcb"), "name" : "liSi", "age" : 15 }
  50. { "_id" : ObjectId("5b98a707496484420f387dcc"), "name" : "liSi", "age" : 16 }
  51. { "_id" : ObjectId("5b98a707496484420f387dcd"), "name" : "liSi", "age" : 17 }
  52. { "_id" : ObjectId("5b98a707496484420f387dce"), "name" : "liSi", "age" : 18 }
  53. { "_id" : ObjectId("5b98a707496484420f387dcf"), "name" : "liSi", "age" : 19 }
  54. { "_id" : ObjectId("5b98a707496484420f387dd0"), "name" : "liSi", "age" : 20 }
  55. { "_id" : ObjectId("5b98a707496484420f387dd1"), "name" : "liSi", "age" : 21 }
  56. Type "it" for more
  57. >

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号