I have a few pairs of bson/json files. I know how to import bson files, but how to do with the json files? Should I import them in a particular order if at all?
You can import JSON files by specifying the type in mongoimport command like this,
mongoimport --db <db name> --collection <collection name> --type json --file example.json --jsonArray
Related
mongoimport --uri mongodb+srv://gtye:<PASSWORD>#dxgraph.rymok.mongodb.net/dxgraph-db --collection dxgraph-collection --type json --file ~/DxHero%20Godot/Data/postgres_export.json
returns
2021-05-25T12:58:05.538-0400 Failed: open /Users/gtye/DxHero%20Godot/Data/postgres_export.json: no such file or directory
2021-05-25T12:58:05.538-0400 0 document(s) imported successfully. 0 document(s) failed to import.
But I know it's there!!
It's literally right here!! What the heck is going on?
%20 is a URI escaping mechanism. It doesn't work in the shell.
Try:
mongoimport --uri mongodb+srv://gtye:<PASSWORD>#dxgraph.rymok.mongodb.net/dxgraph-db --collection dxgraph-collection --type json --file ~/"DxHero Godot/Data/postgres_export.json"
I know that we can use the mongoimport command to insert one json file into a mongodb database:
mongoimport --jsonArray --db [dbname] --collection [collectionname] --file filename.json
But are there any command that let me insert multiple json files at once?
On mac
cd jsonfilesfolder
ls -1 *.json | while read jsonfile; do mongoimport -d shop --file $jsonfile --type json; done
Reference
I am a beginner and trying to import data from MongoDB. The Robo3T which I use for visualization shows something like below.
--Kins
Kins
And this is the command I use
mongoexport --db kins --collection test --out output.json
mongoexport --db kins.kins --collection test --out output.json
mongoexport --db [kins].[kins] --collection test --out output.json
But none of them work. What's the correct syntax?
How to import data in json format. Please guide me.
Link I reviewed:
Dump Mongo Collection into JSON format
I guess you want to use the --jsonArray (docs) flag because by default mongoexport writes data using one JSON document for every MongoDB document.
mongoexport --db kins.kins --collection test --out output.json --jsonArray
You can consider using the --pretty (docs) flag too. It will output documents in pretty-printed format.
I'm using this command
mongoimport --db test --collection bankdata --drop --file bank_data.json --port 27017 --host 127.0.0.1
to import a json file, but when I do it I get this message
Failed: error unmarshaling bytes on document #0: JSON decoder out of sync - data changing underfoot?
2016-05-24T08:00:31.552-0600 imported 0 documents
You are trying to import json array ,so you need to add --jsonArray option according to documentation https://docs.mongodb.com/manual/reference/program/mongoimport/
mongoimport --jsonArray --db test --collection bankdata --drop --file
bank_data.json
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?
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
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
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/
You can use this:
FOR %i IN (<data folder>\*.json) DO mongoimport -d <database> -c <collection> --file %i