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 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 just moved to a new laptop which had mongo 3.0.0 I believe.
On the new laptop I have mongo 3.0.4. and trying the script that was working on the old laptop is giving me errors. This line is giving me the error.
mongoimport --host localhost \
-db roudy123_q \
-collection LebaneseAmericanUniversity\(Lebanon\).json \
--file LebaneseAmericanUniversity\(Lebanon\).json \
--jsonArray
error validating settings: only one positional argument is allowed.
I googled the error and the only relevant result was the source code of mongoimport. So I guess it has something to do with the new version.
Just a wild guess...
... but the various long options should be specified using --, not -:
mongoimport --host localhost \
--db roudy123_q \
--collection LebaneseAmericanUniversity\(Lebanon\).json \
--file LebaneseAmericanUniversity\(Lebanon\).json \
--jsonArray
Maybe this particular version of mongoimport is more punctilious about that, and will treat -db ... -collection ... as positional arguments rather than keyword arguments ?
This error can also occur if white spaces are given without a "\" in the path to the file .
Ex:
This wont work:
But this would work :
If you are getting mongodb-error validating settings: only one positional argument is allowed.
Just put --file path in " ".
2.use / instead of \ in --file path.
Also, put --host in " ".
For example:
Suppose you are trying to import data from your local machine to server (MongoDB Atlas or your MongoDB server or locally) in your collection then follow this:
mongoimport --host "cluster0-shard-00-01-ceax1.mongodb.net:27017" --db <dbname> --type json --file "C:/Users/ranjeet/Downloads/MongoDb project/ranjeet.json" --authenticationDatabase admin --ssl --username <Username> --password <Password> --collection <CollectionName>
If you get this error while inserting fields with --fields, the probable reason might be you are using spaces to do that.
Both -f and --fields should work in those cases
Using Mongo Version 3.0.6
mongoimport --db logs --collection action_logs --type tsv -f updated_at ,transaction_time ,origin --file parsed.tsv
mongoimport --db logs --collection action_logs --type tsv -f updated_at,transaction_time,origin --file parsed.tsv
I think giving a white spaces in the file name of directory will also contribute to this error.
None of the above mentioned answers solved my problem but they indeed helped me in figuring out what I was doing wrong.(I am using windows)
1)using -d instead of --d (shorthand only require one - not two --)
2)using "" for absolute file path.
3)Changing \ to / in file path location.
For example my files location in windows is:
C:\kp github\other projects\projectXyz\myFile.csv
So for me the command that worked was:
mongoimport -d users -c contacts --type=csv --headerline --file="C:/kp github/other projects/projectXyz/myFile.csv" or
mongoimport -d users -c contacts --type csv --headerline --file "C:/kp github/other projects/projectXyz/myFile.csv"
where users is my db name and contacts is my collection name
The below is the correct example of command when your database have user name and password created
mongoimport --host 127.0.0.1
--port 27000
--username XXXXX
--password PPPPP
--authenticationDatabase admin
--db applicationData
--collection products
--file products.json
Please make sure you do not have any extra spaces after
and before -- as well.
I'm trying to import a json file into mongo. When I import the file with this line, it works:
mongoimport -d reps_development -c users --jsonArray --file ~/reps/scripts/mockUserData.json
The script uses an environment variables $REPS_ROOT, which is set in my .bash_profile. This line fails:
mongoimport -d reps_development -c users --jsonArray --file $REPS_ROOT/scripts/mockUserData.json
I set $REPS_ROOT with the following command:
export REPS_ROOT="~/reps"
Any thoughts on why this isn't working? The error I get is:
file doesn't exist: ~/reps/scripts/mockUserData.json
Bash expands $REPS_ROOT to ~/reps, which becomes the value. Bash won't expand that value again. If the value of that variable contains a relative path, you need to make sure it is expanded. For example:
mongoimport -d reps_development -c users --jsonArray --file $(cd $REPS_ROOT; pwd)/scripts/mockUserData.json
If you are on Linux, you can use $(readlink -f $REPS_ROOT) instead. Alternatively you can use $HOME instead of ~:
export REPS_ROOT="$HOME/reps"
I have a JSON file consisting of about 2000 records. Each record which will correspond to a document in the mongo database is formatted as follows:
{jobID:"2597401",
account:"XXXXX",
user:"YYYYY",
pkgT:{"pgi/7.2-5":{libA:["libpgc.so"],flavor:["default"]}},
startEpoch:"1338497979",
runTime:"1022",
execType:"user:binary",
exec:"/share/home/01482/XXXXX/appker/ranger/NPB3.3.1/NPB3.3-MPI/bin/ft.D.64",
numNodes:"4",
sha1:"5a79879235aa31b6a46e73b43879428e2a175db5",
execEpoch:1336766742,
execModify: new Date("Fri May 11 15:05:42 2012"),
startTime: new Date("Thu May 31 15:59:39 2012"),
numCores:"64",
sizeT:{bss:"1881400168",text:"239574",data:"22504"}},
Each record is on a single line in the JSON file, and the only line breaks are at the end of every record. Therefore, each line in the document starts with "{jobID:"... I am trying to import these into a mongo database using the following command:
mongoimport --db dbName --collection collectionName --file fileName.json
However, I get the following error:
Sat Mar 2 01:26:12 Assertion: 10340:Failure parsing JSON string near: ,execModif
0x10059f12b 0x100562d5c 0x100562e9c 0x10025eb98 0x10000e643 0x100010b60 0x10055c4cc 0x1000014b7
0x100001454
0 mongoimport 0x000000010059f12b _ZN5mongo15printStackTraceERSo + 43
1 mongoimport 0x0000000100562d5c _ZN5mongo11msgassertedEiPKc + 204
2 mongoimport 0x0000000100562e9c _ZN5mongo11msgassertedEiRKSs + 12
3 mongoimport 0x000000010025eb98 _ZN5mongo8fromjsonEPKcPi + 1576
4 mongoimport 0x000000010000e643
_ZN6Import8parseRowEPSiRN5mongo7BSONObjERi + 2739
5 mongoimport 0x0000000100010b60 _ZN6Import3runEv + 7376
6 mongoimport 0x000000010055c4cc _ZN5mongo4Tool4mainEiPPc + 5436
7 mongoimport 0x00000001000014b7 main + 55
8 mongoimport 0x0000000100001454 start + 52
Sat Mar 2 01:26:12 exception:BSON representation of supplied JSON is too large: Failure parsing
JSON string near: ,execModif
Sat Mar 2 01:26:12
Sat Mar 2 01:26:12 imported 0 objects
Sat Mar 2 01:26:12 ERROR: encountered 1941 errors
I do not know what the problem is. Can someone recommend a solution?
I was able to fix the error using the following query:
mongoimport --db dbName --collection collectionName --file fileName.json --jsonArray
Hopefully this is helpful to someone.
try this,
mongoimport --db dbName --collection collectionName <fileName.json
Example,
mongoimport --db foo --collection myCollections < /Users/file.json
connected to: *.*.*.*
Sat Mar 2 15:01:08 imported 11 objects
Issue is because of you date format.
I used same JSON with modified date as below and it worked
{jobID:"2597401",
account:"XXXXX",
user:"YYYYY",
pkgT:{"pgi/7.2-5":{libA:["libpgc.so"],flavor:["default"]}},
startEpoch:"1338497979",
runTime:"1022",
execType:"user:binary",
exec:"/share/home/01482/XXXXX/appker/ranger/NPB3.3.1/NPB3.3-MPI/bin/ft.D.64",
numNodes:"4",
sha1:"5a79879235aa31b6a46e73b43879428e2a175db5",
execEpoch:1336766742,
execModify:{"$date" : 1343779200000},
startTime:{"$date" : 1343779200000},
numCores:"64",
sizeT:{bss:"1881400168",text:"239574",data:"22504"}}
hope this helps
Using mongoimport you can able to achieve the same
mongoimport --db test --collection user --drop --file ~/downloads/user.json
where,
test - Database name
user - collection name
user.json - dataset file
--drop is drop the collection if already exist.
console:
mongoimport -d dbName -c collectionName dataFile.js
I have used below command for export DB
mongodump --db database_name --collection collection_name
and below command worked for me to import DB
mongorestore --db database_name path_to_bson_file
Your syntax appears completely correct in:
mongoimport --db dbName --collection collectionName --file fileName.json
Make sure you are in the correct folder or provide the full path.
Import JSON/CSV file in MongoDB
wait wait
first check mongoimport.exe file in your bin folder(C:\Program Files\MongoDB\Server\4.4\bin) if it is not then download mongodb database tools(https://www.mongodb.com/try/download/database-tools)
copy extracted(unzip) files(inside unzipped bin) to bin folder(C:\Program Files\MongoDB\Server\4.4\bin)
copy your json file to bin folder(C:\Program Files\MongoDB\Server\4.4\bin)
Now open your commond prompt change its directory to bin
cd "C:\Program Files\MongoDB\Server\4.4\bin"
Now copy this on your commnad prompt
mongoimport -d tymongo -c test --type json --file restaurants.json
where d- database(tymongo-database name), c-collection(test-collection name)
FOR CSV FILE
mongoimport -d tymongo -c test --type csv --file database2.csv --headerline
Run the import command in another terminal. (not inside mongo shell.)
mongoimport --db test --collection user --drop --file ~/downloads/user.json
In windows you can use your Command Prompcmd cmd , in Ubuntu you can use your terminal by typing the following command:
mongoimport -d your_database_name -c your_collection_name /path_to_json_file/json_file_name.json
then when you open your mongo shell, you will find to check your database_name when running this command:
show databases
This command works where no collection is specified .
mongoimport --db zips "\MongoDB 2.6 Standard\mongodb\zips.json"
Mongo shell after executing the command
connected to: 127.0.0.1
no collection specified!
using filename 'zips' as collection.
2014-09-16T13:56:07.147-0400 check 9 29353
2014-09-16T13:56:07.148-0400 imported 29353 objects
Solution:-
mongoimport --db databaseName --collection tableName --file filepath.json
Example:-
Place your file in admin folder:-
C:\Users\admin\tourdb\places.json
Run this command on your teminal:-
mongoimport --db tourdb --collection places --file ~/tourdb/places.json
Output:-
admin#admin-PC MINGW64 /
$ mongoimport --db tourdb --collection places --file ~/tourdb/places.json
2019-08-26T14:30:09.350+0530 connected to: localhost
2019-08-26T14:30:09.447+0530 imported 10 documents
For more link
I tried something like this and it actually works:
mongoimport --db dbName --file D:\KKK\NNN\100YWeatherSmall.data.json
This works with me when db with usrname and password
mongoimport --db YOUR_DB --collection MyCollection --file /your_path/my_json_file.json -u my_user -p my_pass
db without username password please remove -u my_user -p my_pass
My sample json
{
"_id" : ObjectId("5d11c815eb946a412ecd677d"),
"empid" : NumberInt(1),
"name" : "Rahul"
}
{
"_id" : ObjectId("5d11c815eb946a412ecd677e"),
"empid" : NumberInt(2),
"name" : "Rahul"
}
A bit late for probable answer, might help new people. In case you have multiple instances of database:
mongoimport --host <host_name>:<host_port> --db <database_name> --collection <collection_name> --file <path_to_dump_file> -u <my_user> -p <my_pass>
Assuming credentials needed, otherwise remove this option.
Just copy path of json file like example "C:\persons.json"
go to C:\Program Files\MongoDB\Server\4.2\bin
open cmd on that mongodb bin folder and run this command
mongoimport --jsonArray --db dbname--collection collectionName--file FilePath
example mongoimport --jsonArray --db learnmongo --collection persons --file C:\persons.json
Number of answer have been given even though I would like to give mine command . I used to frequently. It may help to someone.
mongoimport original.json -d databaseName -c yourcollectionName --jsonArray --drop
mongoimport -d <dbname> -c <collectio_name> --file <c:\users\test.json> --jsonArray
If you try to export this test collection:
> db.test.find()
{ "_id" : ObjectId("5131c2bbfcb94ddb2549d501"), "field" : "Sat Mar 02 2013 13:13:31 GMT+0400"}
{"_id" : ObjectId("5131c2d8fcb94ddb2549d502"), "field" : ISODate("2012-05-31T11:59:39Z")}
with mongoexport (the first date created with Date(...) and the second one created with new Date(...) (if use ISODate(...) will be the same as in the second line)) so mongoexport output will be looks like this:
{ "_id" : { "$oid" : "5131c2bbfcb94ddb2549d501" }, "field" : "Sat Mar 02 2013 13:13:31 GMT+0400" }
{ "_id" : { "$oid" : "5131c2d8fcb94ddb2549d502" }, "field" : { "$date" : 1338465579000 } }
So you should use the same notation, because strict JSON doesn't have type Date( <date> ).
Also your JSON is not valid: all fields name must be enclosed in double quotes, but mongoimport works fine without them.
You can find additional information in mongodb documentation and here.
this will work:
$ mongoimport --db databaseName --collection collectionName --file filePath/jsonFile.json
2021-01-09T11:13:57.410+0530 connected to: mongodb://localhost/
2021-01-09T11:13:58.176+0530 1 document(s) imported successfully. 0 document(s) failed to import.
Above I shared the query along with its response