MongoDB的数据库导入和导出
MongoDB的导出
-
//导出为csv格式
-
mongoexport -d dbname -c 集合名称 -
q {} -f filed1,filed2,... --type=csv > filename.csv
-
//导出为json格式
-
mongoexport -d dbname -c 集合名称 -
q {} -f filed1,filed2,... filename.json
-
-
//带条件的导出
-
mongoexport -d dbname -c 集合名称 -
q
'{"age":{"$gt":20}}' -f filed1,filed2,... --type=csv > filename.csv
导出的示例
-
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
-
2018-09-10T10:25:54.582+0800 csv flag is deprecated; please use --
type=csv instead
-
2018-09-10T10:25:54.604+0800 connected to: localhost:27001
-
2018-09-10T10:25:54.610+0800 exported 8 records
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 2018-09-10T10:30:06.945+0800 connected to: localhost:27001 2018-09-10T10:30:06.971+0800 exported 8 records
带条件的导出windows的-q "{}",linux的则是-q '{}'。两者有区别,大家切记。
-
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
-
2018-09-10T10:50:06.952+0800 connected to: localhost:27001
-
2018-09-10T10:50:06.969+0800 exported 2 records
MongoDB的导入
-
//导入csv格式的文件
-
mongoimport -d dbname -c collectionName --
type csv --headerline --drop < filename.csv
-
//导入json格式的文件
-
mongoimport -d dbname -c collectionName --
type json --drop < filename.json
导入的示例
-
D:\
Program
Files\
MongoDB\
Server\4
.0\
bin>
mongoimport
.exe
-h
localhost
:27001
-d
mldn
-c
emps
--type
csv
--headerline
--drop <
emps1
.csv
-
2018
-09-10T11
:00
:09.704+0800
connected
to:
localhost
:27001
-
2018
-09-10T11
:00
:09.721+0800
dropping:
mldn
.emps
-
2018
-09-10T11
:00
:10.991+0800
imported 2
documents
-
D:\Program Files\MongoDB\Server\4.0\bin>mongoimport.exe -h localhost:27001 -d mldn -c emps --
type json --drop < emps.json
-
2018-09-10T11:02:31.431+0800 connected to: localhost:27001
-
2018-09-10T11:02:31.442+0800 dropping: mldn.emps
-
2018-09-10T11:02:32.688+0800 imported 8 documents
检查数据,正常。
-
db.emps.find();
-
{
"_id" : ObjectId(
"5b95de7d2dd48cac697bfbc2"),
"name" :
"孙七",
"age" : 21,
"job" :
"manager",
"salary" : 6000 }
-
{
"_id" : ObjectId(
"5b95de7d2dd48cac697bfbc3"),
"name" :
"李四",
"age" : 10,
"job" :
"clerk",
"salary" : 2000 }
-
{
"_id" : ObjectId(
"5b95de7d2dd48cac697bfbc4"),
"name" :
"钱九",
"age" : 18,
"job" :
"president",
"salary" : 10000 }
-
{
"_id" : ObjectId(
"5b95de7d2dd48cac697bfbc5"),
"name" :
"吴十",
"age" : 19,
"job" :
"clerk",
"salary" : 2000 }
-
{
"_id" : ObjectId(
"5b95de7d2dd48cac697bfbc0"),
"name" :
"王五",
"age" : 25,
"job" :
"manager",
"salary" : 7000 }
-
{
"_id" : ObjectId(
"5b95de7d2dd48cac697bfbc6"),
"name" :
"周八",
"age" : 17,
"job" :
"clerk",
"salary" : 2000 }
-
{
"_id" : ObjectId(
"5b95de7d2dd48cac697bfbbf"),
"name" :
"张三",
"age" : 20,
"job" :
"clerk",
"salary" : 2000 }
-
{
"_id" : ObjectId(
"5b95de7d2dd48cac697bfbc1"),
"name" :
"赵六",
"age" : 20,
"job" :
"clerk",
"salary" : 2000 }
版权声明: 原创文章,如需转载,请注明出处。 https://blog.csdn.net/lwx356481/article/details/82586045