Home > front end >  MONGODB - calculate percentage of employees with mongoDB
MONGODB - calculate percentage of employees with mongoDB

Time:11-14

I have the following dataset. I need to calculate the number of each "job_title": how many "data scientist", "Machine learning Scientist" do I have. The problem is I need to have all the results together in the same table with MongoDB.

/* 1 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292727"),
    "null" : 0,
    "work_year" : 2020,
    "experience_level" : "MI",
    "employment_type" : "FT",
    "job_title" : "Data Scientist",
    "salary" : 70000,
    "salary_currency" : "EUR",
    "salary_in_usd" : 79833,
    "employee_residence" : "DE",
    "remote_ratio" : 0,
    "company_location" : "DE",
    "company_size" : "L"
},

/* 2 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292728"),
    "null" : 1,
    "work_year" : 2020,
    "experience_level" : "SE",
    "employment_type" : "FT",
    "job_title" : "Machine Learning Scientist",
    "salary" : 260000,
    "salary_currency" : "USD",
    "salary_in_usd" : 260000,
    "employee_residence" : "JP",
    "remote_ratio" : 0,
    "company_location" : "JP",
    "company_size" : "S"
},

/* 3 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292729"),
    "null" : 2,
    "work_year" : 2020,
    "experience_level" : "SE",
    "employment_type" : "FT",
    "job_title" : "Big Data Engineer",
    "salary" : 85000,
    "salary_currency" : "GBP",
    "salary_in_usd" : 109024,
    "employee_residence" : "GB",
    "remote_ratio" : 50,
    "company_location" : "GB",
    "company_size" : "M"
},

/* 4 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a6729272a"),
    "null" : 3,
    "work_year" : 2020,
    "experience_level" : "MI",
    "employment_type" : "FT",
    "job_title" : "Product Data Analyst",
    "salary" : 20000,
    "salary_currency" : "USD",
    "salary_in_usd" : 20000,
    "employee_residence" : "HN",
    "remote_ratio" : 0,
    "company_location" : "HN",
    "company_size" : "S"
},

/* 5 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a6729272b"),
    "null" : 4,
    "work_year" : 2020,
    "experience_level" : "SE",
    "employment_type" : "FT",
    "job_title" : "Machine Learning Engineer",
    "salary" : 150000,
    "salary_currency" : "USD",
    "salary_in_usd" : 150000,
    "employee_residence" : "US",
    "remote_ratio" : 50,
    "company_location" : "US",
    "company_size" : "L"
},

/* 6 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a6729272c"),
    "null" : 5,
    "work_year" : 2020,
    "experience_level" : "EN",
    "employment_type" : "FT",
    "job_title" : "Data Analyst",
    "salary" : 72000,
    "salary_currency" : "USD",
    "salary_in_usd" : 72000,
    "employee_residence" : "US",
    "remote_ratio" : 100,
    "company_location" : "US",
    "company_size" : "L"
},

/* 7 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a6729272d"),
    "null" : 6,
    "work_year" : 2020,
    "experience_level" : "SE",
    "employment_type" : "FT",
    "job_title" : "Lead Data Scientist",
    "salary" : 190000,
    "salary_currency" : "USD",
    "salary_in_usd" : 190000,
    "employee_residence" : "US",
    "remote_ratio" : 100,
    "company_location" : "US",
    "company_size" : "S"
},

/* 8 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a6729272e"),
    "null" : 7,
    "work_year" : 2020,
    "experience_level" : "MI",
    "employment_type" : "FT",
    "job_title" : "Data Scientist",
    "salary" : 11000000,
    "salary_currency" : "HUF",
    "salary_in_usd" : 35735,
    "employee_residence" : "HU",
    "remote_ratio" : 50,
    "company_location" : "HU",
    "company_size" : "L"
},

/* 9 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a6729272f"),
    "null" : 8,
    "work_year" : 2020,
    "experience_level" : "MI",
    "employment_type" : "FT",
    "job_title" : "Business Data Analyst",
    "salary" : 135000,
    "salary_currency" : "USD",
    "salary_in_usd" : 135000,
    "employee_residence" : "US",
    "remote_ratio" : 100,
    "company_location" : "US",
    "company_size" : "L"
},

/* 10 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292730"),
    "null" : 9,
    "work_year" : 2020,
    "experience_level" : "SE",
    "employment_type" : "FT",
    "job_title" : "Lead Data Engineer",
    "salary" : 125000,
    "salary_currency" : "USD",
    "salary_in_usd" : 125000,
    "employee_residence" : "NZ",
    "remote_ratio" : 50,
    "company_location" : "NZ",
    "company_size" : "S"
},

/* 11 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292731"),
    "null" : 10,
    "work_year" : 2020,
    "experience_level" : "EN",
    "employment_type" : "FT",
    "job_title" : "Data Scientist",
    "salary" : 45000,
    "salary_currency" : "EUR",
    "salary_in_usd" : 51321,
    "employee_residence" : "FR",
    "remote_ratio" : 0,
    "company_location" : "FR",
    "company_size" : "S"
},

/* 12 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292732"),
    "null" : 11,
    "work_year" : 2020,
    "experience_level" : "MI",
    "employment_type" : "FT",
    "job_title" : "Data Scientist",
    "salary" : 3000000,
    "salary_currency" : "INR",
    "salary_in_usd" : 40481,
    "employee_residence" : "IN",
    "remote_ratio" : 0,
    "company_location" : "IN",
    "company_size" : "L"
},

/* 13 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292733"),
    "null" : 12,
    "work_year" : 2020,
    "experience_level" : "EN",
    "employment_type" : "FT",
    "job_title" : "Data Scientist",
    "salary" : 35000,
    "salary_currency" : "EUR",
    "salary_in_usd" : 39916,
    "employee_residence" : "FR",
    "remote_ratio" : 0,
    "company_location" : "FR",
    "company_size" : "M"
},

/* 14 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292734"),
    "null" : 13,
    "work_year" : 2020,
    "experience_level" : "MI",
    "employment_type" : "FT",
    "job_title" : "Lead Data Analyst",
    "salary" : 87000,
    "salary_currency" : "USD",
    "salary_in_usd" : 87000,
    "employee_residence" : "US",
    "remote_ratio" : 100,
    "company_location" : "US",
    "company_size" : "L"
},

/* 15 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292735"),
    "null" : 14,
    "work_year" : 2020,
    "experience_level" : "MI",
    "employment_type" : "FT",
    "job_title" : "Data Analyst",
    "salary" : 85000,
    "salary_currency" : "USD",
    "salary_in_usd" : 85000,
    "employee_residence" : "US",
    "remote_ratio" : 100,
    "company_location" : "US",
    "company_size" : "L"
},

/* 16 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292736"),
    "null" : 15,
    "work_year" : 2020,
    "experience_level" : "MI",
    "employment_type" : "FT",
    "job_title" : "Data Analyst",
    "salary" : 8000,
    "salary_currency" : "USD",
    "salary_in_usd" : 8000,
    "employee_residence" : "PK",
    "remote_ratio" : 50,
    "company_location" : "PK",
    "company_size" : "L"
},

/* 17 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292737"),
    "null" : 16,
    "work_year" : 2020,
    "experience_level" : "EN",
    "employment_type" : "FT",
    "job_title" : "Data Engineer",
    "salary" : 4450000,
    "salary_currency" : "JPY",
    "salary_in_usd" : 41689,
    "employee_residence" : "JP",
    "remote_ratio" : 100,
    "company_location" : "JP",
    "company_size" : "S"
},

/* 18 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292738"),
    "null" : 17,
    "work_year" : 2020,
    "experience_level" : "SE",
    "employment_type" : "FT",
    "job_title" : "Big Data Engineer",
    "salary" : 100000,
    "salary_currency" : "EUR",
    "salary_in_usd" : 114047,
    "employee_residence" : "PL",
    "remote_ratio" : 100,
    "company_location" : "GB",
    "company_size" : "S"
},

/* 19 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292739"),
    "null" : 18,
    "work_year" : 2020,
    "experience_level" : "EN",
    "employment_type" : "FT",
    "job_title" : "Data Science Consultant",
    "salary" : 423000,
    "salary_currency" : "INR",
    "salary_in_usd" : 5707,
    "employee_residence" : "IN",
    "remote_ratio" : 50,
    "company_location" : "IN",
    "company_size" : "M"
},

/* 20 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a6729273a"),
    "null" : 19,
    "work_year" : 2020,
    "experience_level" : "MI",
    "employment_type" : "FT",
    "job_title" : "Lead Data Engineer",
    "salary" : 56000,
    "salary_currency" : "USD",
    "salary_in_usd" : 56000,
    "employee_residence" : "PT",
    "remote_ratio" : 100,
    "company_location" : "US",
    "company_size" : "M"
},

/* 21 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a6729273b"),
    "null" : 20,
    "work_year" : 2020,
    "experience_level" : "MI",
    "employment_type" : "FT",
    "job_title" : "Machine Learning Engineer",
    "salary" : 299000,
    "salary_currency" : "CNY",
    "salary_in_usd" : 43331,
    "employee_residence" : "CN",
    "remote_ratio" : 0,
    "company_location" : "CN",
    "company_size" : "M"
},

/* 22 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a6729273c"),
    "null" : 21,
    "work_year" : 2020,
    "experience_level" : "MI",
    "employment_type" : "FT",
    "job_title" : "Product Data Analyst",
    "salary" : 450000,
    "salary_currency" : "INR",
    "salary_in_usd" : 6072,
    "employee_residence" : "IN",
    "remote_ratio" : 100,
    "company_location" : "IN",
    "company_size" : "L"
},

/* 23 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a6729273d"),
    "null" : 22,
    "work_year" : 2020,
    "experience_level" : "SE",
    "employment_type" : "FT",
    "job_title" : "Data Engineer",
    "salary" : 42000,
    "salary_currency" : "EUR",
    "salary_in_usd" : 47899,
    "employee_residence" : "GR",
    "remote_ratio" : 50,
    "company_location" : "GR",
    "company_size" : "L"
},

/* 24 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a6729273e"),
    "null" : 23,
    "work_year" : 2020,
    "experience_level" : "MI",
    "employment_type" : "FT",
    "job_title" : "BI Data Analyst",
    "salary" : 98000,
    "salary_currency" : "USD",
    "salary_in_usd" : 98000,
    "employee_residence" : "US",
    "remote_ratio" : 0,
    "company_location" : "US",
    "company_size" : "M"
},

/* 25 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a6729273f"),
    "null" : 24,
    "work_year" : 2020,
    "experience_level" : "MI",
    "employment_type" : "FT",
    "job_title" : "Lead Data Scientist",
    "salary" : 115000,
    "salary_currency" : "USD",
    "salary_in_usd" : 115000,
    "employee_residence" : "AE",
    "remote_ratio" : 0,
    "company_location" : "AE",
    "company_size" : "L"
},

/* 26 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292740"),
    "null" : 25,
    "work_year" : 2020,
    "experience_level" : "EX",
    "employment_type" : "FT",
    "job_title" : "Director of Data Science",
    "salary" : 325000,
    "salary_currency" : "USD",
    "salary_in_usd" : 325000,
    "employee_residence" : "US",
    "remote_ratio" : 100,
    "company_location" : "US",
    "company_size" : "L"
},

/* 27 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292741"),
    "null" : 26,
    "work_year" : 2020,
    "experience_level" : "EN",
    "employment_type" : "FT",
    "job_title" : "Research Scientist",
    "salary" : 42000,
    "salary_currency" : "USD",
    "salary_in_usd" : 42000,
    "employee_residence" : "NL",
    "remote_ratio" : 50,
    "company_location" : "NL",
    "company_size" : "L"
},

/* 28 createdAt:12/11/2022 13:02:36*/
{
    "_id" : ObjectId("636f8b5cbe2ead3a67292742"),
    "null" : 27,
    "work_year" : 2020,
    "experience_level" : "SE",
    "employment_type" : "FT",
    "job_title" : "Data Engineer",
    "salary" : 720000,
    "salary_currency" : "MXN",
    "salary_in_usd" : 33511,
    "employee_residence" : "MX",
    "remote_ratio" : 0,
    "company_location" : "MX",
    "company_size" : "S"
},

I did an account for each one, but dont know how to do it all together.

CodePudding user response:

You should use $group, on job_title field:

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

Playground link.

  • Related