Reputation: 57
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
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