8

Following the Getting started with MongoDB tutorial at

https://docs.mongodb.org/getting-started/node/import-data/

Trying to import the example database using the given code

mongoimport --db test --collection restaurants --drop --file primer-dataset.json

I get

2015-07-09T21:52:17.396+1200 E QUERY    SyntaxError: Unexpected identifier

I am running this in a bash shell with mongod running in another shell. I have tried copying the data several times in case it was a problem with the json file, and even substituting the data with a very simple json file but the problem persists. Can someone help, please.

8
  • Try this from console> mongoimport -d test -c restaurants primer-dataset.js Commented Jul 9, 2015 at 10:24
  • Still same error message E Query SyntaxError: Unexpected identifier Commented Jul 9, 2015 at 10:30
  • are you running the command from the terminal (and not from inside the mongo shell) ?? The mongoimport which you have written looks fine.The command shouldn't be run from inside the mongo shell, but outside of it. Commented Jul 9, 2015 at 10:35
  • If I run the command outside of a mongo shell I get Failed: open primer-dataset.json no such file or directory. I put it in the root and in the data/db folder as I wasn't sure where it should go. Commented Jul 9, 2015 at 10:40
  • no need to put it in the data/db directory. you can put it in any other folder and cd to that directory and then run the above command Commented Jul 9, 2015 at 11:20

10 Answers 10

14

You command is correct. Use it from the terminal and not inside the mongo shell. However, I would make a small tweak to your mongoimport as below just to make it generic: On the terminal :

mongoimport --db test --collection restaurants --drop --file /<complete-path>/primer-dataset.json

So, if file is in /myimports folder then it would be :

mongoimport --db test --collection restaurants --drop --file /myimports/primer-dataset.json
Sign up to request clarification or add additional context in comments.

2 Comments

As I commented to @gpullen above the problem was the filename primer-dataset.json. It works with non-hyphenated filenames only which is weird Mongodb naming conventions promote hyphens between words and I don't see OS-X has anything against it. Thanks.
It's good that you got it working :). Btw, I tried doing the mongoimport with the same name 'primer-dataset.json' and it worked fine. So, I am not sure if the hyphenated filenames is an issue. (I am using Mongo 3.0.4)
4

You should quit() mongo.exe at first.

Second input mongoimport --db test --collection restaurants --drop --file D:/MongoDB/data/dataset.json.

Comments

2

I faced the same issue. Although, none of the above solutions helped. After trawling the internet, I have finally found the solution.

In my case, the problem was the absence of the mongoimport.exe file in the bin directory.

So, follow the next steps:

  1. Download the MongoDB Database Tools https://www.mongodb.com/try/download/database-tools.
  2. Copy all files from the bin directory to the following directory - MongoDB\Server\4.4\bin.

Now, the mongoimport has to be working.

Comments

0

Ran into same issue, the instructions in the online documentation you listed are good as long as you change the case of your Downloads folder (Windows is not case sensitive but Linux is) in your command. If yours is like mine, you have to change

mongoimport --db test --collection restaurants --drop --file ~/downloads/primer-dataset.json

to

mongoimport --db test --collection restaurants --drop --file ~/Downloads/primer-dataset.json

Also as anish mentioned you do have to run this in the terminal (not inside mongo shell) the mongoimport connects to the a mongod instance running on localhost on port 27017

1 Comment

This was my issue, too. I changed downloads to Downloads and it ran perfectly. Sometimes it's the simple things that get us! Thanks Justin.
0

quit() mongo.exe because it shouldn't be written in mongo terminal

type

.\mongoimport --db test --collection restaurants --drop --file primer-dataset.json

This should work!

1 Comment

Please, expand your answer with a description of why to do this actions and add some formatting.
0

You don't run mongoimport from mongo shell, run it directly from your OS terminal

Comments

0

This is what I tried and it works well !!

What you can do is just convert your json object to an array using any editor and then use --jsonArray and --legacy at the end of your uri. Will Work fine .

mongoimport --db test --collection restaurants --drop --file primer-dataset.json --jsonArray --legacy

Remember to convert your json Object to an array before tyring this

Comments

0
  1. Download mongotools, then copy files to your /bin folder.
  2. Make sure you have a right PATH in windows to /bin folder and mongo is working.
  3. Open CMD and paste: mongoimport --db yourDbName --collection nameOfCollection ^ --username <user> --password <password> ^ --drop --file filePath
  4. Test with MONGOSHELL: db.nameOfCollection.find()

Comments

0

I solved this problem by eliminating the spaces from the json file. Let's say your json file looks like this:

    {
"created_at": "01/01/2021",
"id": 1050118621198921720,
"text": "To make room for more expression”
"user": {
"id": 6253282,
"name": "Bertrand",
"location": "San Francisco",
"followers_count": 613
}
}
{
"created at": "29/03/2021",
"id": 1050118621198921729,
"text": "salut Tunisie”
"user": {
"id": 6253559,
"name": "Jhon",
"location": "London",
"followers_count": 89
}
}
{
"created at": "25/10/2021",
"id": 1050118621198921721,
"text": "Essai”
"user": {
"id": 6253553,L’énoncé de l’examen est clair, aucune clarification n’est permise au cours de l’épreuve !
"name": "Philippe",
"location": "Paris",
"followers_count": 6133636
}
}
{
"created_at": "29/03/2021",
"id": 1050118621198921728,
"text": "test”
"user": {
"id": 6253284,
"name": "Patrick",
"location": "Marseille",
"followers_count": 33636
}
}

It must look like this:

{"created_at": "01/01/2021","id": 1050118621198921720,"text": "To make room for more expression","user": {"id": 6253282,"name": "Bertrand","location": "San Francisco","followers_count": 613}}{"created at": "29/03/2021","id": 1050118621198921729,"text": "salut Tunisie","user": {"id": 6253559,"name": "Jhon","location":"London","followers_count": 89}}{"created at": "25/10/2021","id": 1050118621198921721,"text": "Essai","user": {"id": 6253553,"name": "Philippe","location": "Paris","followers_count": 6133636}}{"created_at": "29/03/2021","id": 1050118621198921728,"text": "test","user": {"id": 6253284,"name": "Patrick","location": "Marseille","followers_count": 33636}}

Comments

0

Here is the process:

  1. Download Mongo tools
  2. Move all the .exe files in the tools folder to the bin folder of your MongoDB in the the location where you installed MongoDB
  3. Open a CMD and move the address to the bin folder of MongoDB (cd C:\Program Files\MongoDB\Server\5.0\bin)
  4. Execute your import command (mongoimport --db databaseName --collection collectionName --drop --file filePath)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.