# Importing OSM Data into MongoDB?

Can anyone tell me how to import OSM data in mongoDB step by step ?

this is the error i am getting in the 1 step:

Unable to open datasource with the following drivers ... [huge list]

So this is how I do it:
1.) convert the osm.pbf file to geojson with ogr2ogr like this:

ogr2ogr -f  GeoJSON points.json C:\data\osm\karlsruhe-regbez-latest.osm.pbf points


the only trick is that you have to enter this statement five times, changing the type of the geometry at the end of the statement :

points / lines / multilinestrings / multipolygons / other_relations

now you have five .json files

2.) Edit each file by deleting these parts:

• the first two lines of the file:
{"type": "FeatureCollection",
"features": [

• the last two lines of the file:
]
}

• and the comma at the end of each row
this step I do with the search&replace feature of notepad++, like this:

(the number of the brackets may vary for different geometry types)

so now you have five json files which can be imported to mongodb

3.) import the json data to mongodb
in the commander cd to mongodb/bin and type:

> mongoimport --db dbname --collection points  --file C:\data\path\to\points.json


Here you can also put all 5 files in one collection.

I know this sounds a lot of work at first, but I did not find any other way which would be quicker.
I hope this helps :)

EDIT
I found a software with which you can do it in one step: osm2mongo
Please note that for this you need to download the osm.bz2 format, not the osm.pbf one. After download you should extract it, so you have a karlsruhe-regbez-latest.osm file.
Then in the commander cd to the osm2mongo.exe and type

>osm2mongo -i karlsruhe-regbez-latest.osm -h localhost:27017 -d dbname


The drawback is, that it has a bit different data structure. It creates 3 collections: nodes, relations and ways.
But it is definitely easier to import with this software in just one step.

• osm2mongo in this can u explain bit clear i downloaded the file after that ..? – user3783320 Aug 9 '14 at 9:58
• Why do I need all the 5 files? Any reasons? – sparkr Oct 13 '15 at 19:40
• ogr2ogr sorts the osm data into 5 categories based on their geometry type. If you only need points for example, then of course import only the points. – ylka Oct 14 '15 at 8:40

Only thing that I know is Osmo but IMHO the best/best supported method is using PostgreSQL as it's most supported solution by the OSM tools.

This can be done using the mongosm importer.

2. Extract the file. Try f.ex. 7zip, or bunzip2 karlsruhe-regbez-latest.osm.bz2 in a shell.
3. Download the repo. Either from me (patched), or from the original and replace Connection with MongoClient.
4. python mongosm-master/insert_osm_data.py karlsruhe-regbez-latest.osm imports the data.