Trying to import a JSON file
Mongodb Version -- V3.6.3
Shell Version 3.6
Using this command to connect to cluster and import
mongoimport --host cluster0-shard-00-00-xxm0e.mongodb.net:24537 --db ny --type json --file C:/data/docs/ny.json --jsonArray --authenticationDatabase admin --ssl --username xyz --password mongodb
error message
2018-03-03T22:53:45.477-0800 no collection specified
2018-03-03T22:53:45.478-0800 using filename 'ny' as collection
2018-03-03T22:53:46.062-0800 Failed: error connecting to db server: server` returned error on SASL authentication step: bad auth Authentication failed.
2018-03-03T22:53:46.062-0800 imported 0 documents
Have tried a few options like
removing -ssl
adding ----sslAllowInvalidCertificates
Adding " C:/data/docs/ny.json" to file name
and none of these works.. Any help appreciated
You have to specify the collection -c or --collection flag. see official docs.
mongoimport
--host <HOST>
--ssl --username <USERNAME>
--password <PASSWORD>
--authenticationDatabase admin
--db <DATABASE>
--collection <COLLECTION>
--type <FILETYPE>
--file <FILENAME>
You missed specifying the collection name :
mongoimport --host cluster0-shard-00-00-xxm0e.mongodb.net:24537 --db ny --collection <entercollectionName> --type json --file C:/data/docs/ny.json --jsonArray --authenticationDatabase admin --ssl --username xyz --password mongodb
try the command now
For now, we can easy import/export without having to have multiple options like this:
mongoimport --uri mongodb+srv://<USERNAME>:<PASSWORD>#your-cluster.mongodb.net/<DATABASE_NAME> --collection <COLLECTION> --type <FILETYPE> --file <FILENAME>
mongoexport --uri mongodb+srv://<USERNAME>:<PASSWORD>#your-cluster.mongodb.net/<DATABASE_NAME> --collection <COLLECTION> --type <FILETYPE> --out <FILENAME>
You also can found these on Your Cluster > Cmd Line Tools
Related
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'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 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 am trying to mongoexport a collection named,BrowserHistory from my DB. I have tried doing this through SSH into my CentOS server and a local MongoDB install on my computer. I have MongoDB 2.6 installed.
Note: I have obscured the server, etc. info
The commands I have tried and results are below:
mongoexport --host host.xysz.com:27017 --username iamuser2 --password securepass --db test1ng1 --collection BrowserHistory --out ram.json
Result: "Error parsing command line: too many positional options have
been specified on the command line try 'mongoexport --help' for more
information"
mongoexport --host host.xysz.com:27017 --username iamuser2 --password securepass --db test1ng1 --out ram.json
Result: connected to: host.xysz.com:27017 no collection specified!
Export MongoDB data to CSV, TSV, or JSON files.
I need to output the collection into json file(s) so I can import it into Hive.
Looks like the 'collection' parameter value has whitespace.
This can be overcome by enclosing it in single quote.
mongoexport --host host.xysz.com:27017 --username iamuser2 --password securepass --db test1ng1 --collection 'BrowserHistory' --out ram.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