MongoDB 语法错误意外令牌

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24062123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:44:31  来源:igfitidea点击:

MongoDB Syntax Error Unexpected Token

mongodbsyntax

提问by clever_bassi

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

我是 MongoDB 的新手。我只是在关注tutorialspoint.com 来学习 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 {

我收到错误 SyntaxError: Unexpected Token {

Any help is appreciated. Thanks.

任何帮助表示赞赏。谢谢。

回答by Shahid Hussain

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

您需要在圆括号中包含花括号,例如 ({}) 第二件事将 id 括在引号中,请参阅 mongodb 手册

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

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

回答by utkarsh-k

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 :

我在 mongo shell 之外并执行以下命令。当我乘地铁前往办公室时,我正在使用智能手机并尝试更新记录。除非我看到“bash”并知道我是多么的无知,否则我从来没有觉得自己脱离了外壳。顺便说一句,下面是我试图执行的错误查询:

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

karsh45:~/workspace $ db.users.updateOne({name:"dummyUser4"}, {$set:{email:"[email protected]"}}); bash:意外标记附近的语法错误`{name:"dummyUser4"},'

回答by Pelle M?rtenson

If you cut and paste a possible error can be that your texteditor has changed the type of quotes. Took me a while to notice that the first " in front of Manhattan are the typografic kind.

如果你剪切和粘贴一个可能的错误可能是你的文本编辑器改变了引号的类型。我花了一段时间才注意到曼哈顿前面的第一个“是印刷字体”。

db.clubs.update({clubname: ”Manhattan"}, {$push: {visitors: {women: 0}}})

The syntax is correct but those " give the error Unexpected Token

语法是正确的,但那些 " 给出错误 Unexpected Token