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

179
Views
How to aggregate results on a common property value?

I have a collection of persons with a group property. Group value is a random string, which I don't know when I make my query. Some persons group values will possibly be the same.

I want to write a query to retrieve only one person per group (which person is returned in the group is non relevant for the moment).

I suppose I have to write some kind of aggregation. But I only read about examples aggregating on known values... Instead I need to aggregate on unknown values...

My schema is something like:

const schemaPersons = new Schema({
  name: {
    type: String,
    required: true,
  },
  group: {
    type: String,
  },
};
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can write aggregation query using $group stage:

  • $group stage, group by group field, and get only first person's root document in person field
Persons.aggregate([
  {
    $group: {
      _id: "$group",
      person: { $first: "$$ROOT" }
    }
  }
]);

Playground

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!