mongo query - SyntaxError:意外的令牌:

时间:2016-06-09 16:55:37

标签: mongodb

我的藏品中有一份文件。

"_id" : ObjectId("5755b94abcfc6666d6f5fe56"),
"NAME" : John Smith,
"PHONE" : "800-555-1000",
"DOC" : {
    " TYPE" : "M",
    " DATE" : "01/28/2016",
    " AMOUNT" : 281593
},
"ADDRESS" : 123 Sesame Street

我试图通过传递键和值来找到这条记录:

db.acris.find("DOC" : {" TYPE" : "MTGE"," DATE" : "01/28/2003"," AMOUNT" : 281593})

但是,我收到以下错误:

SyntaxError: Unexpected token :

如何修复此查询以使代码正常工作?

2 个答案:

答案 0 :(得分:1)

你在整个发现中错过了{}

db.collection.find({ "DOC" : ... })

更新: 只是为了完整。您还可以搜索整个元素。你不一定需要点符号。

> db.test.insertOne({"NAME" : "John Smith",
... "PHONE" : "800-555-1000",
... "DOC" : {
...     " TYPE" : "M",
...     " DATE" : "01/28/2016",
...     " AMOUNT" : 281593
... },
... "ADDRESS" : "123 Sesame Street"})

> db.test.find({"DOC" : { " TYPE" : "M", " DATE" : "01/28/2016", " AMOUNT" : 281593 }})
{ "_id" : ObjectId("5759a933cdcc5a8d09a1aeb9"), "NAME" : "John Smith", "PHONE" : "800-555-1000", "DOC" : { " TYPE" : "M", " DATE" : "01/28/2016", " AMOUNT" : 281593 }, "ADDRESS" : "123 Sesame Street" }

答案 1 :(得分:1)

语法错误修正为KRONWALLED答案显示。我认为你想要的查询是:

db.acris.find({"DOC.TYPE" : "M", "DOC.DATE" : "01/28/2003"," DOC.AMOUNT" : 281593})

注意与子字段匹配的DOT表示法。