MongoDB的数据库导入和导出

MongoDB的数据库导入和导出

MongoDB的导出


   
   
   
   
  1. //导出为csv格式
  2. mongoexport -d dbname -c 集合名称 - q {} -f filed1,filed2,... --type=csv > filename.csv
  3. //导出为json格式
  4. mongoexport -d dbname -c 集合名称 - q {} -f filed1,filed2,... filename.json
  5. //带条件的导出
  6. mongoexport -d dbname -c 集合名称 - q '{"age":{"$gt":20}}' -f filed1,filed2,... --type=csv > filename.csv

导出的示例


   
   
   
   
  1. D:\Program Files\MongoDB\Server\4.0\bin>mongoexport.exe -h localhost:27001 -d mldn -c emps -q {} -f name,age,job,salary --csv > emps.csv
  2. 2018-09-10T10:25:54.582+0800 csv flag is deprecated; please use -- type=csv instead
  3. 2018-09-10T10:25:54.604+0800 connected to: localhost:27001
  4. 2018-09-10T10:25:54.610+0800 exported 8 records

   
   
   
   
  1. D:\Program Files\MongoDB\Server\4.0\bin>mongoexport.exe -h localhost:27001 -d mldn -c emps -q {} -f name,age,job,salary > emps.json
  2. 2018-09-10T10:30:06.945+0800 connected to: localhost:27001
  3. 2018-09-10T10:30:06.971+0800 exported 8 records

带条件的导出windows的-q "{}",linux的则是-q '{}'。两者有区别,大家切记。


   
   
   
   
  1. D:\Program Files\MongoDB\Server\4.0\bin>mongoexport.exe -h localhost:27001 -d mldn -c emps -q "{age:{$gt:20}}" -f name,age,job,salary -- type=csv > emps1.csv
  2. 2018-09-10T10:50:06.952+0800 connected to: localhost:27001
  3. 2018-09-10T10:50:06.969+0800 exported 2 records

MongoDB的导入


   
   
   
   
  1. //导入csv格式的文件
  2. mongoimport -d dbname -c collectionName -- type csv --headerline --drop < filename.csv
  3. //导入json格式的文件
  4. mongoimport -d dbname -c collectionName -- type json --drop < filename.json

导入的示例


   
   
   
   
  1. D:\ Program Files\ MongoDB\ Server\4 .0\ bin> mongoimport .exe -h localhost :27001 -d mldn -c emps --type csv --headerline --drop < emps1 .csv
  2. 2018 -09-10T11 :00 :09.704+0800 connected to: localhost :27001
  3. 2018 -09-10T11 :00 :09.721+0800 dropping: mldn .emps
  4. 2018 -09-10T11 :00 :10.991+0800 imported 2 documents

   
   
   
   
  1. D:\Program Files\MongoDB\Server\4.0\bin>mongoimport.exe -h localhost:27001 -d mldn -c emps -- type json --drop < emps.json
  2. 2018-09-10T11:02:31.431+0800 connected to: localhost:27001
  3. 2018-09-10T11:02:31.442+0800 dropping: mldn.emps
  4. 2018-09-10T11:02:32.688+0800 imported 8 documents

检查数据,正常。


   
   
   
   
  1. db.emps.find();
  2. { "_id" : ObjectId( "5b95de7d2dd48cac697bfbc2"), "name" : "孙七", "age" : 21, "job" : "manager", "salary" : 6000 }
  3. { "_id" : ObjectId( "5b95de7d2dd48cac697bfbc3"), "name" : "李四", "age" : 10, "job" : "clerk", "salary" : 2000 }
  4. { "_id" : ObjectId( "5b95de7d2dd48cac697bfbc4"), "name" : "钱九", "age" : 18, "job" : "president", "salary" : 10000 }
  5. { "_id" : ObjectId( "5b95de7d2dd48cac697bfbc5"), "name" : "吴十", "age" : 19, "job" : "clerk", "salary" : 2000 }
  6. { "_id" : ObjectId( "5b95de7d2dd48cac697bfbc0"), "name" : "王五", "age" : 25, "job" : "manager", "salary" : 7000 }
  7. { "_id" : ObjectId( "5b95de7d2dd48cac697bfbc6"), "name" : "周八", "age" : 17, "job" : "clerk", "salary" : 2000 }
  8. { "_id" : ObjectId( "5b95de7d2dd48cac697bfbbf"), "name" : "张三", "age" : 20, "job" : "clerk", "salary" : 2000 }
  9. { "_id" : ObjectId( "5b95de7d2dd48cac697bfbc1"), "name" : "赵六", "age" : 20, "job" : "clerk", "salary" : 2000 }

版权声明: 原创文章,如需转载,请注明出处。 https://blog.csdn.net/lwx356481/article/details/82586045

你可能感兴趣的:(转载系列)