2

I want to get the max and min value of the emp salary based on currency. every employee has a salary range based on currency also all the details in response should be unique. while I am using aggregation function min and max but it fetches the max and min value of salary amount but I need to get max and min based on currency field.

Sample Data:

[
  {
    "id": "1",
    "emp_name": "emp1",
    "data": [
      {
        "emp_country": "country1",
        "emp_city": "city1",
        "salary": [
          {
            "currency": "INR",
            "amount": 5000
          },
          {
            "currency": "INR",
            "amount": 600
          },
          {
            "currency": "MXN",
            "amount": 400
          }
        ]
      },
      {
        "emp_country": "country1",
        "emp_city": "city2",
        "salary": [
          {
            "currency": "DOLLER",
            "amount": 5000
          },
          {
            "currency": "DOLLER",
            "amount": 200
          },
          {
            "currency": "MXN",
            "amount": 400
          }
        ]
      }
    ]
  },
  {
    "id": "2",
    "emp_name": "emp2",
    "data": [
      {
        "emp_country": "country2",
        "emp_city": "city2",
        "salary": [
          {
            "currency": "INR",
            "amount": 5000
          },
          {
            "currency": "MXN",
            "amount": 200
          },
          {
            "currency": "MXN",
            "amount": 400
          }
        ]
      }
    ]
  },
  {
    "id": "3",
    "emp_name": "emp3",
    "data": [
      {
        "emp_country": "country1",
        "emp_city": "city1",
        "salary": [
          {
            "currency": "MXN",
            "amount": 400
          }
        ]
      }
    ]
  },
  {
    "id": "4",
    "emp_name": "emp4",
    "data": [
      {
        "emp_country": "country1",
        "emp_city": "city2",
        "salary": [
          {
            "currency": "DOLLER",
            "amount": 200
          }
        ]
      }
    ]
  }
]

Expected Output: city, country, the name should be unique and salary have max and min based on currency.

[
  {
    "emp_city": "city1",
    "emp_country": "country1",
    "emp_name": "emp1",
    "emp_salary": [{
      "currency": "INR",
      "max": 5000,
      "min": 600
    },
    {
      "currency": "MXN",
      "max": 400,
      "min": 400
    }]
  },
  {
    "emp_city": "city2",
    "emp_country": "country1",
    "emp_name": "emp1",
    "emp_salary":[ {
      "currency": "DOLLER",
      "max": 5000,
      "min": 200
    },
    {
      "currency": "MXN",
      "max": 400,
      "min": 400
    }]
  },
  {
    "emp_city": "city2",
    "emp_country": "country2",
    "emp_name": "emp2",
    "emp_salary": [{
      "currency": "INR",
      "max": 5000,
      "min": 5000
    },
    {
      "currency": "MXN",
      "max": 400,
      "min": 200
    }]
  },
  {
    "emp_city": "city1",
    "emp_country": "country1",
    "emp_name": "emp3",
    "emp_salary": [{
      "currency": "MXN",
      "max": 400,
      "min": 400
    }]
  },
  {
    "emp_city": "city2",
    "emp_country": "country1",
    "emp_name": "emp4",
    "emp_salary": [{
      "currency": "DOLLER",
      "max": 200,
      "min": 200
    }]
  }
]
2
  • your sample data is one doc in your collection or there are multiple doc in collection Commented Aug 31, 2021 at 9:16
  • @mohammadNaimi sample data is 4 doc in the collection while id is manually generated rather than automatic. Commented Aug 31, 2021 at 10:16

1 Answer 1

4

We can achieve the above result using an aggregation query.

  • $unwind - to deconstruct array of salary
  • $group - group by emp city, country, name, currency with min and max value of salary
  • $group - use another to push in emp_salary
  • $project - to show fields in your format
db.collection.aggregate([
  {
    "$unwind": {
      "path": "$data"
    }
  },
  {
    "$unwind": {
      "path": "$data.salary"
    }
  },
  {
    "$group": {
      "_id": {
        "emp_city": "$data.emp_city",
        "emp_country": "$data.emp_country",
        "emp_name": "$emp_name",
        "currency": "$data.salary.currency",
        
      },
      "max": {
        "$max": "$data.salary.amount"
      },
      "min": {
        "$min": "$data.salary.amount"
      }
    }
  },
  {
    "$group": {
      "_id": {
        "emp_city": "$_id.emp_city",
        "emp_country": "$_id.emp_country",
        "emp_name": "$_id.emp_name",
        
      },
      "emp_salary": {
        "$push": {
          "currency": "$_id.currency",
          "min": "$min",
          "max": "$max"
        }
      }
    }
  },
  {
    "$project": {
      "_id": 0,
      "emp_city": "$_id.emp_city",
      "emp_country": "$_id.emp_country",
      "emp_name": "$_id.emp_name",
      "emp_salary": 1,
      
    }
  }
])

Mongo Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.