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"
Related
I apologize if my question does not deserve a standard to ask here.
I have two files(products-data.json, orders-data.json) inside the following directory:
G:\kb\Couchbase\CB121
and I imported the products-data.json successfully using the following command:
G:\kb\Couchbase\CB121>cbimport.exe json -c couchbase://127.0.0.1 -u sattar -p 156271 -b sampleDB -f lines -d file://products-data.json -t 4 -g %type%::%variety%::#MONO_INCR#
But when I try to import orders-data.json in the same way as follows:
G:\kb\Couchbase\CB121>cbimport.exe json -c couchbase://127.0.0.1 -u sattar -p 156271 -b sampleDB -f lines -d file://orders-data.json -t 4 -g %type%::%order_id%
I am getting the following error:
2018-01-21T12:01:31.211+06:00 [31mERRO: open orders-data.json: The system cannot find the file specified.[0m[2m -- jsondata.(*Parallelizer).Execute() at source.go:198[0m
2018-01-21T12:01:31.212+06:00 [31mERRO: open orders-data.json: The system cannot find the file specified.[0m[2m -- plan.(*data).execute() at data.go:89[0m
Json import failed: open orders-data.json: The system cannot find the file specified.
It kills my day. Any help is appreciated. Thanks.
It is the first time I use mongodb.
I want to insert a zip.json file into mongodb running one of the commands bellow, and I get error.
mongoimport --db test --collection zips --file \C:\Install\advanced programs\mongodb\zips.json
mongoimport --db test --collection zips --type json --file \C:\Install\advanced programs\mongodb\zips.json
The error:
To run these commands I oppened a cmd in bin directory of mongodb and I ran mongo.
you should use double quotation "" for file path because your file path contained space and no need to extra back slash \C:\ just use C:\
use
mongoimport --db test --collection zips --file "C:\Install\advanced programs\mongodb\zips.json"
instead of
mongoimport --db test --collection zips --file \C:\Install\advanced programs\mongodb\zips.json
Perhaps your json is not valid? Looks like there is an extra quote somewhere. Try validating it using any online service.
You need to start different cmd and go to C:\Program Files\MongoDB\Server\3.2\bin or it should be in ur path and then u need to run "mongoimport" command. If you start using "mongoimport" from "mongo" shell then it will not work.
I'm trying to import json document using mongoimport command and have tried all possible ways but it's giving be below error. Plz tell me what m I doing wrong here. I've given full path of json doc. I want to import big document having 800+ records but because it failing so currently my students.json contains simple one line {name : "Archana"} but even this is failing.
C:\data\db>mongo
2016-02-02T17:48:44.788+0530 I CONTROL [main] Hotfix KB2731284 or later update is installed, no need to zero-out data
MongoDB shell version: 3.2.1
connecting to: test
> show collections
names
students
> mondoimport -d test -c students students.json
2016-02-02T17:50:21.001+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:15
> mondoimport -d test -c students < students.json
2016-02-02T17:50:25.840+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:15
> mondoimport -d test -c students < file students.json
2016-02-02T17:50:31.233+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:15
> mondoimport -d test -c students < f students.json
2016-02-02T17:50:35.417+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:15
> mondoimport -d test -c students < f C:\data\db\students.json
2016-02-02T17:50:51.658+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:15
> mondoimport -d test -c students < f "C:\data\db\students.json"
2016-02-02T17:50:56.897+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:15
> mondoimport -d test -c students C:\data\db\students.json
2016-02-02T17:51:06.849+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:15
> mondoimport -db test -c students C:\data\db\students.json
2016-02-02T17:54:33.545+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:16
> mondoimport --db test --collection students C:\data\db\students.json
2016-02-02T17:56:24.253+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:14
> mondoimport --db test --collection students --file C:\data\db\students.json
2016-02-02T17:56:33.589+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:14
> mondoimport --db test --collection students --file C:\data\db\students.json --jsonArray
2016-02-02T17:58:21.131+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:14
> mondoimport --db test --collection students C:\data\db\students.json --jsonArray
2016-02-02T17:58:32.650+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:14
You are probably running this command under mongo shell. As you can see in the path program files/mongodb/server/3.2/bin there is no command under mongod as mongoimport. So in order to solve the problem, navigate your cmd prompt to the file and without using mongo shell(mongo) run the command in cmd prompt.
I was trying to use mongoimport from mongo shell. So answer is "mongoimport is different utility" so if you want to use mongoimport then after starting mongo server using "mongod", use need to start different cmd and go to C:\Program Files\MongoDB\Server\3.2\bin or it should be in ur path and then u need to run "mongoimport" command. If you start using "mongoimport" from "mongo" shell then it will not work.
mongoimport is different utility so use different command prompt.
Try the following steps in the given manner:
Step1. On new command prompt, cd to location of mongo's bin directory.
for example:
cd c:\MongoDB\bin
Step2. Run the following command:
mongoimport --db test --collection restaurants --file location/of/jsonfile
try like this. copy past this code in text document and save it with .sh extension. place both the files on desktop and execute .sh file in terminal
mongoimport --db test --collection students --file students.json
Hi you need to do the following things:
Save the JSON file in a directory, e.g.: C:/Users/DEEPU/students.json
Open a command prompt and go to that directory by using the cd command where your file is saved.
Then type mongoimport --drop -d students -c grades grades.json
Click the below link to see it working.
bin>mongoexport -d databasename -c collectionName
-o export.json
The same works for mongorestore too. If you need to restore a db or its schema you need to go to the path where the .zip file is and then run the mongorestore command. mongorestore <filename>
if you are using windows machine then,open new command prompt and try like this:
mongoimport.exe -h localhost:27017 -d sampleTestDB -c testc -u "" -p "" D:\mongoSample.json
Note: if you not set the PATH, then set the path and then try this.
how to set the path?
==> in Environment Variables, select PATH then click on "edit" copy your mongodb installed path as shown here and save it
C:\Program Files\MongoDB\Server\3.4\bin
I was also facing the same problem on my ubuntu 16.04. I misunderstood, I was running inside MongoDB shell. mongoimport is utility tool. You just need to open new terminal and run mongoimport command not inside the mongodb shell.
mongoimport --db <dbname> --collection zips --drop --file <location of file>
I was also doing the same error in windows 10.
Yes , mongoimport should be executed outside of mongo shell .
Please check mongo installation path till bin in system path, else it will throw error.
And then execute below command from command window [not mongo shell]
mongoimport --db testdb --collection mytable --file "D:\Emp.json"
First, navigate to the folder where mongoexport.exe is located in your system in my case it is in (C:\Program Files\MongoDB\Server\4.0\bin). Now Execute the mongoimport command on the cmd terminal, not on the mongo shell. the mongoimport command should not be executed on mongo shell.
Execute :
C:\Program Files\MongoDB\Server\4.0\bin>mongoimport --db project --collection users --file data.json
In linux:
$./mongoimport --db project --collection users --file data.json
Here data.json file is in the same directory where mongoimport is situated otherwise we have to provide path/location to the file.
it seems you are trying to run the cmd after the mongod cmd.
> sudo mongoexport --db envyd -c users --out /var/www/api/dump_beta/users.json
2021-05-05T09:09:18.779+0000 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:5
from the folder use like this
ubuntu#ip-172-11-11-186:~$ sudo mongoexport --db envyd -c users --out /var/www/api/dump_beta/users.json
2021-05-05T09:11:01.059+0000 connected to: localhost
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 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