15.1 C
London
Friday, July 5, 2024
HomeMongoDBAggregation in MongoDBMongoDB: How to Select Distinct Values from Multiple Fields

MongoDB: How to Select Distinct Values from Multiple Fields

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following syntax to select distinct values from multiple fields in MongoDB:

db.collection.aggregate( 
            [
                {$group: { "_id": { field1: "$field1", field2: "$field2" } } }
            ]
        )

The following examples show how to use this syntax with a collection teams with the following documents:

db.teams.insertOne({team: "Mavs", position: "Guard", points: 31})
db.teams.insertOne({team: "Mavs", position: "Guard", points: 22})
db.teams.insertOne({team: "Rockets", position: "Center", points: 19})
db.teams.insertOne({team: "Rockets", position: "Forward", points: 26})
db.teams.insertOne({team: "Rockets", position: "Forward", points: 29}) 
db.teams.insertOne({team: "Cavs", position: "Guard", points: 33})

Example: Select Distinct Values from Multiple Fields

We can use the following query to find all of the distinct values from the team and position fields:

db.teams.aggregate( 
            [
                {$group: { "_id": { team: "$team", position: "$position" } } }
            ]
        )

This query returns the following documents:

{ _id: { team: 'Mavs', position: 'Guard' } }
{ _id: { team: 'Rockets', position: 'Forward' } }
{ _id: { team: 'Rockets', position: 'Center' } }
{ _id: { team: 'Cavs', position: 'Guard' } }

And we can use the following query to find all of the distinct values from the team, position, and points fields:

db.teams.aggregate( 
        [
          {$group: {"_id": {team: "$team", position: "$position", points: "$points"}}}
        ]
    )

This query returns the following documents:

{ _id: { team: 'Cavs', position: 'Guard', points: 33 } }
{ _id: { team: 'Rockets', position: 'Forward', points: 29 } }
{ _id: { team: 'Mavs', position: 'Guard', points: 22 } }
{ _id: { team: 'Rockets', position: 'Forward', points: 26 } }
{ _id: { team: 'Mavs', position: 'Guard', points: 31 } }
{ _id: { team: 'Rockets', position: 'Center', points: 19 } }

Notice that all of the documents are returned since there are no two documents that have identical values for the team, position, and points fields.

Additional Resources

The following tutorials explain how to perform other common operations in MongoDB:

MongoDB: How to Add a New Field in a Collection
MongoDB: How to Group By and Count
MongoDB: How to Group By Multiple Fields

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories