13

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
Archana Mundaye
  • 523
  • 1
  • 5
  • 19
  • sorry for that but mongoimport is also failing – Archana Mundaye Feb 02 '16 at 12:54
  • 1
    `mongoimport` tool should be run from a separate shell – chridam Feb 02 '16 at 12:59
  • sorry for that but mongoimport is also failing giving same error. – Archana Mundaye Feb 02 '16 at 13:00
  • 4
    Launch another cmd shell then run the import separately from the `mongo` shell i.e. in a separate cmd window do `C:\data\db>mongoimport -d test -c students students.json` – chridam Feb 02 '16 at 13:02
  • in one shell I started mongod using "mongod" process then opened 2nd shell and typed "mongo" then it showed message as "connecting to: test" then from there I typed mongoimport command. are you saying I should oped 3rd command prompt? – Archana Mundaye Feb 02 '16 at 13:03
  • Yes it worked thank you so much..I was frustrated with that....but why it does not recognize mongoimport from that shell. – Archana Mundaye Feb 02 '16 at 13:07
  • 1
    `mongoimport` is not a command but another utility tool that resides in the same path as `mongo.exe`. All you need to do is open another terminal and run the tool separately. – chridam Feb 02 '16 at 13:07
  • ok.. got you. thank a lot. – Archana Mundaye Feb 02 '16 at 13:08
  • Just because there is no excepted answer here, I get this same response when trying to enter a shutdown command in the shell. I have tried everything listed on this mongodb web site https://docs.mongodb.org/manual/reference/command/shutdown/ including { shutdown: 1, force: true } but I get the same error response. [thread1] SyntaxError: missing ; before statement @(shell):1:20 except for this last value is 20 not 15. – htm11h Mar 09 '16 at 14:51

13 Answers13

40

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.

Gaurava Agarwal
  • 974
  • 1
  • 9
  • 32
Akshay Seth
  • 1,224
  • 1
  • 12
  • 11
6

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.

Archana Mundaye
  • 523
  • 1
  • 5
  • 19
  • if the answer is "mongoimport is different utility" then maybe accept the answer where you've literally quoted that from (even if it's your own, and not particularly detailed) instead of leaving this unresolved? Alternatively accept @Akshay's answer, which is more detailed. – Owen Jul 22 '16 at 19:08
5

mongoimport is different utility so use different command prompt.

Archana Mundaye
  • 523
  • 1
  • 5
  • 19
4

mongoimport command

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
Robin James Kerrison
  • 1,727
  • 1
  • 15
  • 26
3

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
Raj
  • 31
  • 6
3

Hi you need to do the following things:

  1. Save the JSON file in a directory, e.g.: C:/Users/DEEPU/students.json
  2. Open a command prompt and go to that directory by using the cd command where your file is saved.
  3. Then type mongoimport --drop -d students -c grades grades.json

Click the below link to see it working.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
Deepak
  • 470
  • 1
  • 3
  • 15
0

bin>mongoexport -d databasename -c collectionName -o export.json

Soshiv Upreti
  • 101
  • 1
  • 6
0

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>

0

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
Vinod Poorma
  • 419
  • 1
  • 6
  • 15
0

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>

Mukesh Kumar Gupta
  • 1,567
  • 20
  • 15
0

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"
0

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.

alphacat
  • 61
  • 1
  • 4
0

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
Mohit Sharma
  • 529
  • 1
  • 6
  • 17