Missing "]" after element list in MongoDB

2020-03-26 mongodb count aggregation-framework

Here's the code:

db.employee.aggregate([
    {$facet: 
        {"total": [$count: "total"]}
    }, 
    {$project:
        {"total": 1}
    }
])
2020-03-26T09:55:35.634+0000 E  QUERY    [js] uncaught exception: SyntaxError: missing ] after element list :
@(shell):1:49

As far as I can see, I'm not missing any brackets. The code is just me testing some stuff btw while I'm learning, that's why it looks like I've gone the long way around.

Answers

Seems like some curly brackets are missing:

{"total": [$count: "total"]} -> {"total": [{$count: "total"}]}

db.employee.aggregate([
    {$facet: 
        {"total": [
            {$count: "total"}
        ]}
    }, 
    {$project:
        {"total": 1}
    }
])

Related