Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
How to exclude _id from mongoose result?

I am getting issue to get the desired output like this

    [
        {
            
             "status": "ONBOARD"
              "count": 1
        },
       {
            
             "status": "PENDING"
              "count": 1
        },
        
    ]

I am getting like this:

[
    {
        "_id": {
            "status": "ONBOARD"
        },
        "count": 1
    },
    {
        "_id": {
            "status": "CLIENT_ROUND"
        },
        "count": 2
    },
    {
        "_id": {
            "status": "SCREENED"
        },
        "count": 1
    }
]

here is my aggregate :

const result = await Lead.aggregate([

      {
        $group: {
          _id: { status: '$status' },
          count: { $sum: 1 }
        }
        $project: { _id: 0, status: 1, count: 1 }
      }

Help me to get the output I am new i have applied $project but giving me error

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You're almost there. With status: "$_id.status" in the projection stage.

db.collection.aggregate([
  {
    $group: {
      _id: {
        status: "$status"
      },
      count: {
        $sum: 1
      }
    }
  },
  {
    $project: {
      _id: 0,
      status: "$_id.status",
      count: 1
    }
  }
])

Sample Mongo Playground


Or just $group with _id: "$status".

db.collection.aggregate([
  {
    $group: {
      _id: "$status",
      count: {
        $sum: 1
      }
    }
  },
  {
    $project: {
      _id: 0,
      status: "$_id",
      count: 1
    }
  }
])

Sample Mongo Playground 2

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!