本文主要介绍MongoDB的导入导出操作。

一、导入数据

MongoDB支持csv格式的数据导入。本节除了导入MongoDB CookBook书本提供的测试数据,还介绍了其他格式的csv文件数据导入。

[root@hdp04 ~]# wget https://raw.githubusercontent.com/kishorek/India-Codes/master/csv/pincodes.csv
[root@hdp04 ~]# head pincodes.csv 
"PostOfficeName","Pincode","DistrictsName","City","State"
"Aberdeen Bazar","744104","A&N Islands","Port Blair","Andaman Nicobar"
"Bakultala","744208","A&N Islands","Port Blair","Andaman Nicobar"
"Bambooflat","744107","A&N Islands","Port Blair","Andaman Nicobar"
"Betapur","744201","A&N Islands","Port Blair","Andaman Nicobar"
[root@hdp04 ~]# mongoimport --type csv -d admin -c postalCodes --headerline --drop pincodes.csv
2018-06-06T15:34:41.922+0800    connected to: localhost
2018-06-06T15:34:41.922+0800    dropping: admin.postalCodes
2018-06-06T15:34:42.981+0800    imported 39734 documents
> db.postalCodes.count()
39734

如果csv文件中存在空字段的内容,则需在导入的时候指定每个字段名称,如下:

[root@hdp04 ~]# tail temp.csv 
"zy-2-3106915","candon",,"2","zy14031238480","Pp2d8+VJOoPOUP2UVMBS4g==","520000","520300",,"1",,,,
"zy-2-3106931","jason",,"2","zy14031238484","+3xWTli/pbRaswx0XxALPg==","520000","520300",,"1",,,,
"zy-2-3106958","mako",,"2","zy14031238434","j8+Fz3EkRH+3JtWKNmScmA==","520000","520300",,"1",,,,
"zy-2-3731038","lanqi",,"2","zy160300038106","6N1phcF07vGhDv7Vof3CPA==","520000","520300",,"1",,,,
"zy-2-3175835","wgjk",,"2","zy14031352290","wxU1xIQbTAmWoR+q9LrLcw==","520000","520300",,"1",,,,
[root@hdp04 ~]# mongoimport --db admin --collection temp --type csv --fields ID,NAME,FILED1,USERTYPE,PHONE,FILED2,FILED3,FILED4,PHONE1,TYPE,FILED5,FILED6,FILED7,FILED8 --file temp.csv
2018-06-10T18:25:32.739+0800    connected to: localhost
2018-06-10T18:25:35.734+0800    [#.......................] admin.temp   8.93MB/177MB (5.0%)
2018-06-10T18:25:38.736+0800    [##......................] admin.temp   17.6MB/177MB (9.9%)
2018-06-10T18:25:41.733+0800    [###.....................] admin.temp   26.2MB/177MB (14.8%)
2018-06-10T18:25:44.733+0800    [####....................] admin.temp   34.8MB/177MB (19.7%)
2018-06-10T18:25:47.733+0800    [#####...................] admin.temp   43.4MB/177MB (24.5%)
2018-06-10T18:25:50.734+0800    [#######.................] admin.temp   53.4MB/177MB (30.1%)
2018-06-10T18:25:53.733+0800    [########................] admin.temp   62.9MB/177MB (35.5%)
2018-06-10T18:25:56.736+0800    [##########..............] admin.temp   75.5MB/177MB (42.6%)
2018-06-10T18:25:59.734+0800    [###########.............] admin.temp   84.2MB/177MB (47.6%)
2018-06-10T18:26:02.734+0800    [############............] admin.temp   93.0MB/177MB (52.5%)
2018-06-10T18:26:05.733+0800    [#############...........] admin.temp   102MB/177MB (57.4%)
2018-06-10T18:26:08.733+0800    [##############..........] admin.temp   110MB/177MB (62.2%)
2018-06-10T18:26:11.734+0800    [################........] admin.temp   119MB/177MB (67.2%)
2018-06-10T18:26:14.733+0800    [#################.......] admin.temp   128MB/177MB (72.3%)
2018-06-10T18:26:17.733+0800    [##################......] admin.temp   137MB/177MB (77.6%)
2018-06-10T18:26:20.733+0800    [###################.....] admin.temp   146MB/177MB (82.7%)
2018-06-10T18:26:23.733+0800    [#####################...] admin.temp   156MB/177MB (87.9%)
2018-06-10T18:26:26.733+0800    [######################..] admin.temp   164MB/177MB (92.9%)
2018-06-10T18:26:29.734+0800    [#######################.] admin.temp   175MB/177MB (99.1%)
2018-06-10T18:26:30.197+0800    [########################] admin.temp   177MB/177MB (100.0%)
2018-06-10T18:26:30.197+0800    imported 3347522 documents
> db.temp.count()
3347522

二、简单查询

  1. 统计行数
> db.postalCodes.count()
39734

2.查询一条记录

> db.postalCodes.findOne()
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c95b"),
        "PostOfficeName" : "Aberdeen Bazar",
        "Pincode" : 744104,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}

3.查找多行记录

> db.postalCodes.find().pretty()
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c95b"),
        "PostOfficeName" : "Aberdeen Bazar",
        "Pincode" : 744104,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c95c"),
        "PostOfficeName" : "Campbell Bay",
        "Pincode" : 744302,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c95d"),
        "PostOfficeName" : "Betapur",
        "Pincode" : 744201,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c95e"),
        "PostOfficeName" : "Car Nicobar",
        "Pincode" : 744301,
        "DistrictsName" : "Car Nicobar",
        "City" : "Car Nicobar",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c95f"),
        "PostOfficeName" : "Diglipur",
        "Pincode" : 744202,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c960"),
        "PostOfficeName" : "Haddo",
        "Pincode" : 744102,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c961"),
        "PostOfficeName" : "Hut Bay",
        "Pincode" : 744207,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c962"),
        "PostOfficeName" : "Havelock",
        "Pincode" : 744211,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c963"),
        "PostOfficeName" : "Bambooflat",
        "Pincode" : 744107,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c964"),
        "PostOfficeName" : "Kapanga",
        "Pincode" : 744304,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c965"),
        "PostOfficeName" : "Long Island",
        "Pincode" : 744203,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c966"),
        "PostOfficeName" : "Marine Jetty",
        "Pincode" : 744101,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c967"),
        "PostOfficeName" : "Mayabunder",
        "Pincode" : 744204,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c968"),
        "PostOfficeName" : "N S Building",
        "Pincode" : 744101,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c969"),
        "PostOfficeName" : "Nancowrie",
        "Pincode" : 744303,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c96a"),
        "PostOfficeName" : "Neel Island",
        "Pincode" : 744104,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c96b"),
        "PostOfficeName" : "New Secretariate Bldg",
        "Pincode" : 744105,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c96c"),
        "PostOfficeName" : "Bakultala",
        "Pincode" : 744208,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c96d"),
        "PostOfficeName" : "Oralkatcha",
        "Pincode" : 744210,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
{
        "_id" : ObjectId("5b178e91f02ce24e2dd4c96e"),
        "PostOfficeName" : "Port Blair",
        "Pincode" : 744101,
        "DistrictsName" : "A&N Islands",
        "City" : "Port Blair",
        "State" : "Andaman Nicobar"
}
Type "it" for more

上面的命令查找所有记录,默认只显示前20行,如果要查看更多的记录,键入“it”继续查看(只显示20条记录)。

三、导出数据

MongoDB的mongoexport默认是导出collection的所有内容,如下:

[root@hdp04 ~]# mongoexport -d admin -c postalCodes -o dd.csv
2018-06-11T11:40:19.660+0800    connected to: localhost
2018-06-11T11:40:20.658+0800    [#########...............]  admin.postalCodes  16000/39734  (40.3%)
2018-06-11T11:40:21.658+0800    [###################.....]  admin.postalCodes  32000/39734  (80.5%)
2018-06-11T11:40:21.873+0800    [########################]  admin.postalCodes  39734/39734  (100.0%)
2018-06-11T11:40:21.873+0800    exported 39734 records

如果要导出指定的字段内容,需要加上--fields参数,如下:

[root@hdp04 ~]# mongoexport -d admin -c postalCodes -o ee.csv --fields 'PostOfficeName,Pincode,DistrictsName'
2018-06-11T11:41:33.805+0800    connected to: localhost
2018-06-11T11:41:34.803+0800    [#########...............]  admin.postalCodes  16000/39734  (40.3%)
2018-06-11T11:41:35.495+0800    [########################]  admin.postalCodes  39734/39734  (100.0%)
2018-06-11T11:41:35.495+0800    exported 39734 records