Outline

  1. Get ready for your GeoJSON data
  2. Convert GeoJSON data to JsonArray data for importation
  3. Use MongoDB command line tool to import data
  4. Finally, import the data successfully and find them

Tutorial

1. Get ready for your GeoJSON data

GeoJSON is a format for encoding a variety of geographic data structures. A GeoJSON object may represent a geometry, a feature, or a collection of features. GeoJSON supports the following geometry types: Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection.

The codes below demonstrate a simple GeoJSON file, which is constructed with 4-point features. You could copy the text and paste into your text editor and save it with the .geojson suffix, and load the file you saved into a GIS application like QGIS or ArcMap.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{
"type": "FeatureCollection",
"name": "earthquakes",
"crs": {
"type": "name",
"properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" }
},
"features": [
{
"type": "Feature",
"properties": {
"DateTime": "1970/01/04 17:00:40.20",
"Latitude": 24.139,
"Longitude": 102.503,
"Depth": 31.0,
"Magnitude": 7.5,
"MagType": "Ms",
"NbStations": 90,
"Gap": null,
"Distance": null,
"RMS": 0.0,
"Source": "NEI",
"EventID": 1970010440
},
"geometry": { "type": "Point", "coordinates": [102.503, 24.139] }
},
{
"type": "Feature",
"properties": {
"DateTime": "1970/01/06 05:35:51.80",
"Latitude": -9.628,
"Longitude": 151.458,
"Depth": 8.0,
"Magnitude": 6.2,
"MagType": "Ms",
"NbStations": 85,
"Gap": null,
"Distance": null,
"RMS": 0.0,
"Source": "NEI",
"EventID": 1970010640
},
"geometry": { "type": "Point", "coordinates": [151.458, -9.628] }
},
{
"type": "Feature",
"properties": {
"DateTime": "1970/01/08 17:12:39.10",
"Latitude": -34.741,
"Longitude": 178.568,
"Depth": 179.0,
"Magnitude": 6.1,
"MagType": "Mb",
"NbStations": 59,
"Gap": null,
"Distance": null,
"RMS": 0.0,
"Source": "NEI",
"EventID": 1970010840
},
"geometry": { "type": "Point", "coordinates": [178.568, -34.741] }
},
{
"type": "Feature",
"properties": {
"DateTime": "1970/01/10 12:07:08.60",
"Latitude": 6.825,
"Longitude": 126.737,
"Depth": 73.0,
"Magnitude": 6.1,
"MagType": "Mb",
"NbStations": 91,
"Gap": null,
"Distance": null,
"RMS": 0.0,
"Source": "NEI",
"EventID": 1970011040
},
"geometry": { "type": "Point", "coordinates": [126.737, 6.825] }
}
]
}

2. Convert GeoJSON data to JsonArray data for importation

The key thing to importing GeoJSON data into Mongodb is the file conversion from GeoJSON to JsonObject or JsonArray. In addition, there are a lot of ways to do so, but, I want to tell you the best way by using a tool called jq. ( jq Official Website here!)

When you finish your download, you could do the conversion by using the command below. Importantly, you must put your own configuration text to replace the text between the square brackets and remove the square brackets finally. Then, try it yourself.

1
jq --compact-output ".features" '[Your GeoJSON File]' > '[GeoJSON File converted]'

3. Use mongodb command line tool to import data

In this step, you should check your system environment config and make sure the MongoDB client or command line tools have been installed well firstly. Then, you could use the command below and replace the content between square brackets as you do well in the previous step.

1
mongoimport -u '[Your MongoDB User]' --password '[Your Password]' --db '[Database to Import]' -c '[Collection to Import]' --file '[Path to Your GeoJSON File]' --jsonArray '[MongoDB Connection String]'

Here is the positive result that tells you which MongoDB has been connected and how many documents have been imported.

1
2
connected to: mongodb://***********
4 document(s) imported successfully. 0 document(s) failed to import.

4. Finally, import the data successfully and find them

Use MongoDB Client to find or query the data from the collection you have created. And, here is the result.

Query Result

References & Documentations

GeoJSON Documentation

https://geojson.org/geojson-spec

MongoDB geospatial

https://www.mongodb.com/docs/manual/geospatial-queries/#std-label-geospatial-geojson

jq-tools Offical Website

https://stedolan.github.io/jq/

本文采用CC-BY-SA-3.0协议,转载请注明出处
Author: 徐奕峰 Eephone Xu