mongoimport json file syntax
Asked Answered
D

13

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
Dodge answered 2/2, 2016 at 12:39 Comment(9)
sorry for that but mongoimport is also failingDodge
mongoimport tool should be run from a separate shellRhinoscopy
sorry for that but mongoimport is also failing giving same error.Dodge
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.jsonRhinoscopy
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?Dodge
Yes it worked thank you so much..I was frustrated with that....but why it does not recognize mongoimport from that shell.Dodge
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.Rhinoscopy
ok.. got you. thank a lot.Dodge
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 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.Aguedaaguero
U
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.

Underwear answered 11/7, 2016 at 6:43 Comment(1)
Mark it as an answer if that helped youUnderwear
D
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.

Dodge answered 17/3, 2016 at 12:28 Comment(1)
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.Bookworm
D
5

mongoimport is different utility so use different command prompt.

Dodge answered 16/3, 2016 at 14:26 Comment(0)
D
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
Disposition answered 27/9, 2016 at 9:14 Comment(1)
how far back did you go on your directory? I am in the root and getting no such file or directoryPiscary
W
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
Wenona answered 2/2, 2016 at 13:24 Comment(1)
essentially opening in different command prompt as said by Chridam, yes it worked.Dodge
F
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.

Fronton answered 13/6, 2017 at 16:31 Comment(1)
Thanks Sebastián PalmaFronton
C
0

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

Crosspurpose answered 14/7, 2017 at 5:6 Comment(0)
E
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>

Echo answered 30/1, 2018 at 10:54 Comment(0)
P
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
Phantasm answered 3/8, 2018 at 7:25 Comment(0)
I
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>

Inkblot answered 19/10, 2018 at 13:2 Comment(0)
B
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"
Bandler answered 8/12, 2018 at 3:5 Comment(0)
H
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.

Hemidemisemiquaver answered 13/7, 2019 at 4:44 Comment(0)
E
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
Earley answered 5/5, 2021 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.