Peadar08
Peadar08

Reputation: 57

How to add text to a MongoDB count query

I'm trying to get a text output added to query to count the number of employed personnel in a DB. I'm using income exists and an indicator that personnel is employed.

My input is

db.collection.countDocuments({income{$exists:true)}, {as: {"Number Employed:"}})

Any thoughts on why this isn't working?

Upvotes: 0

Views: 124

Answers (1)

Joe
Joe

Reputation: 28366

The countDocuments function returns a number, not a document. The second argument is an options object, not a projection. The valid options are limit, skip, hint, and maxTimeMS.

If you need to return a document with a specific field name, use aggregation:

db.collection.aggregate([
    {$match: {income{$exists:true}}},
    {$count: "Number Employed"}
])

Upvotes: 1

Related Questions