Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Syntax Error Unexpected Token

Tags:

syntax

mongodb

I am new to MongoDB. I am just following tutorialspoint.com for learning mongoDB.

I executed these two commands exactly as given :

db.test.save( { a: 1 } )
db.test.find(){ "_id" : ObjectId(5879b0f65a56a454), "a" : 1 }

I am getting error SyntaxError: Unexpected Token {

Any help is appreciated. Thanks.

like image 260
clever_bassi Avatar asked Dec 25 '22 08:12

clever_bassi


2 Answers

Your query:

 test.find() { "_id" : ObjectId(5879b0f65a56a454), "a" : 1 }

Correct query:

 test.find( { "_id" : ObjectId("5879b0f65a56a454"), "a" : 1 })

you need to include curly braces in the round brackets like ({}) second thing enclose id in quotes, please refer the mongodb manual

http://docs.mongodb.org/manual/reference/method/db.collection.find/

like image 160
Shahid Hussain Avatar answered Jan 15 '23 21:01

Shahid Hussain


Something stupid I did that got me into this type of error, I would like to share here :

I was OUTSIDE of mongo shell and executing below command. I was using my smartphone and trying to update a record while I was travelling to my office via metro. I never felt for a minute that I was out of the shell unless I saw 'bash' and got to know how ignorant I was. By the way below is the query I was trying to execute with error :

karsh45:~/workspace $ db.users.updateOne({name:"dummyUser4"}, {$set:{email:"[email protected]"}}); bash: syntax error near unexpected token `{name:"dummyUser4"},'

like image 30
utkarsh-k Avatar answered Jan 15 '23 19:01

utkarsh-k