Javascript SyntaxError:应为表达式,Get';}'@(外壳):mongoDB中的1:0
我在命中shell上的查询时得到了Javascript SyntaxError:应为表达式,Get';}'@(外壳):mongoDB中的1:0,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我在命中shell上的查询时得到了SyntaxError:expected表达式,得到了'}'@(shell):1:0 db.address.aggregate([ { "$project": { "applications": { "$filter": { "input": "$applications", "as": "applic
SyntaxError:expected表达式,得到了'}'@(shell):1:0
db.address.aggregate([
{
"$project": {
"applications": {
"$filter": {
"input": "$applications",
"as": "applications",
"cond": {
"$or": [
{
"$and": [
{"$in": ['122018', "$$applications.code"]},
{"$eq": ["$$applications.name", "DSB"]},
]
},
{
"$and": [
{"$in": ['122018', "$$applications.code"]},
{"$eq": ["$$applications.name", "DSB"]},
]
}
]
}
}
},
name: true
}
},
{$unwind: {path: "$applications", preserveNullAndEmptyArrays: false}}
])
syntex错误在哪里问题不在于语法,而在于执行查询的方式。在shell中,尝试删除所有空白行并以单行运行,它将工作。例如
db.address.aggregate([ { "$project": { "applications": { "$filter": { "input": "$applications", "as": "applications", "cond": { "$or": [ { "$and": [ {"$in": ['122018', "$$applications.code"]}, {"$eq": ["$$applications.name", "DSB"]}, ] }, { "$and": [ {"$in": ['122018', "$$applications.code"]}, {"$eq": ["$$applications.name", "DSB"]}, ] } ] } } }, name: true } }, {$unwind: {path: "$applications", preserveNullAndEmptyArrays: false}} ])
或者更好的方法是,使用json文件并将json文件作为查询运行。提醒,如果使用json文件,请记住引用所有字段(例如
$unwind
),并在每个位置使用双引号,而不是简单的引号