user944513
user944513

Reputation: 12729

SyntaxError: expected expression, got '}' @(shell):1:0 in mongoDB

I am getting SyntaxError: expected expression, got '}' @(shell):1:0 while hitting the query on 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}}
])

where is the syntex error

Upvotes: 2

Views: 4749

Answers (2)

rBay
rBay

Reputation: 91

The problem is a very common syntax error in mongo js. Double line break is not interpreted correctly by mongo. Remove the blank lines in l.24 and l.25 and try again.

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}}])

Upvotes: 0

ndmhta
ndmhta

Reputation: 536

The problem is not in the Syntax but the way you are executing the query. In the shell, try removing all the blank lines and run in a single line, it will work. E.g.

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}} ])

Or better yet, use a json file and run the json file as query.

Upvotes: 3

Related Questions