I have a lot of json files in archive and i need to import them into mongo per one operation (i think that it might be in cycle). Have you any ideas about this?
Asked
Active
Viewed 2.2k times
13
-
In which operating system you want to import ? – Sumeet Kumar Yadav Mar 04 '14 at 09:13
-
I want to import in Win7 – user3306125 Mar 04 '14 at 09:17
4 Answers
25
If you are in a Linux/Unix shell you can try
for filename in *; do mongoimport -d mydb -c $filename; done
If you are on Windows:
FOR %i IN (C:\mongodbData\*.json) DO mongoimport --db dbName --collection colection --type json --file %i

Елин Й.
- 953
- 10
- 25

Sumeet Kumar Yadav
- 11,912
- 6
- 43
- 80
-
Well, how it works?:) I'm copy this into the shell, edit db and col. Where i need to point a folder with jsons? @Sumeet – user3306125 Mar 04 '14 at 09:54
-
i have updated the answer. You can write windows path in IN parameter .@user3306125 – Sumeet Kumar Yadav Mar 04 '14 at 09:59
-
-
2But this is importing everything in single collection i.e 'collection', what if there are the different collections with different names.? I would like to create collections with json file names without extension – BeeBee8 Apr 18 '18 at 11:53
-
error parsing command line options: expected argument for flag `/file' – Denis G. Labrecque Feb 01 '21 at 18:30
-
this script works however it connected to mongodb client so in case of importing thousands of files it takes quite a lot of time. is there anyway to speed up the script? – Phạm Nhật Minh Apr 05 '23 at 07:15
4
mongorestore is import all exported mongodb files
cd C:\Program Files\MongoDB\Server\4.0\bin
mongorestore.exe -d <db name> C:\Users\Mike\Downloads\myProject\
But if you really want to import all only meta json files without .bson
cd C:\Users\Mike\Downloads\myProject\
FOR %i IN (*.json) DO "C:\Program Files\MongoDB\Server\4.0\bin\mongoimport.exe" --db <db name> --collection %~ni --type json --file %i
This is sample work on windows 10

Михаил Кудрявцев
- 111
- 1
- 4
3
You need to use mongorestore
for recovery from dump, created by the mongodump
http://docs.mongodb.org/v2.6/reference/program/mongorestore/
for example
mongorestore --drop --oplogReplay mongodb/

Hett
- 3,484
- 2
- 34
- 51
0
You can use this:
FOR %i IN (<data folder>\*.json) DO mongoimport -d <database> -c <collection> --file %i

Ahmed Hesham
- 19
- 3