Home:ALL Converter>How to Import Multiple JSON Files to Mongodb?

How to Import Multiple JSON Files to Mongodb?

Ask Time:2013-07-26T04:31:26         Author:Onuray Sahin

Json Formatter

I am newbie on Mongodb. Actually I have thousands of files in different folders. All of files include json data. There are more than 30 millions files. So I think the best way to store this data is document based db.

I am aware of Import more than 1 json file using mongoimport this SO post. However, accepted answer require a collection which has file names in it. I cannot put 30 millions file name in a collection...

How can I import multiple json files to Mongodb on Windows env?

Author:Onuray Sahin,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/17868193/how-to-import-multiple-json-files-to-mongodb
Majdoub Wided :

I was looking for the solution for 2 days and here is the solution which works for me:\n\nC:\\MongoDB\\Server\\3.0\\bin>\n for %i in (C:\\test\\*) do \n mongoimport --file %i --type json --db mydb --collection mycollection\n\n\nYou just copy and paste this code in the cmd and change the file directories C:\\MongoDB\\Server\\3.0\\binand C:\\test\\.",
2019-08-15T07:39:23
Derick :

You will need to write a script in your favourite language that reads each file, JSON-decodes it and then inserts them one by one into MongoDB. In PHP, such a script would be akin to:\n\n<?php\n$f = glob(\"*.json\");\n$m = new MongoClient;\n$c = $m->myDb->myCollection;\n\nforeach ( $f as $fileName )\n{\n $contents = json_decode( file_get_contents( $fileName ) );\n $c->insert( $contents );\n}\n?>\n",
2013-07-26T10:10:16
user2321465 :

You can create a batch script which fetches all json file in a given folder and then imports it to the db:\n\n@echo off\nfor %%f in (*.json) do (\n\"mongoimport.exe\" --jsonArray --db databasename --collection collectioname --file %%~nf.json )\n\n\nHope this helps",
2016-03-20T19:05:39
yy