Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb unwind array nested inside an array of documents

In MongoDB, I need to be able to unwind nested an array in a document inside an array inside the main document.

{
    "_id" : ObjectId("5808d700536d1a3d69f4cf51"),
    "last_name" : "Maity",
    "xiith_mark" : 58,
    "id" : "3539488",
    "first_name" : "Harshavardhan",
    "course_name" : "BE/B.Tech",
    "institute_name_string" : "Abhayapuri College, P.O. Abhayapuri",
    "profile_percentage" : 45,
    "xiith_mark_type" : "Percentage",
    "xth_mark_type" : "Percentage",
    "date_of_birth" : "14-April-1993",
    "xth_mark" : 30,
    "last_login" : 1470827224,
    "percentage" : 55,
    "job_details" : [
        {
            "status" : NumberLong(6),
            "applied_date" : NumberLong(1470831441),
            "job_id" : NumberLong(92928),
            "contact_viwed_status" : 0,
            "label_name" : [
                "shortlisted",
                "rejected"
            ],
            "questionnaire_status" : 0,
            "batch_id" : NumberLong(6),
            "call_letter" : NumberLong(812)
        }, 
        {
            "status" : NumberLong(6),
            "applied_date" : NumberLong(1470831441),
            "job_id" : NumberLong(92928),
            "contact_viwed_status" : 0,
            "label_name" : [
                "shortlisted",
                "rejected"
            ],
            "questionnaire_status" : 0,
            "batch_id" : NumberLong(6),
            "call_letter" : NumberLong(812)
        }
    ],
    "branch_name" : "Applied Electronics",
    "candidate_state_name" : "West Bengal",
    "candidate_city_name_string" : "Kolkata",
    "10" : 10,
    "12" : 12,
    "skills" : "",
    "gender" : "Male",
    "fw_id" : "FW15884830",
    "cgpa" : 0,
    "picture_path" : "",
    "hq_passout_year" : 2019
}

Based on the record above I need to count the job labels (job_details.label_name).

I have tried the following query:

db.response.aggregate(
    {"$match":type_match},
    {"$unwind": "$job_details" }, 
    {"$group": 
      {
        "_id":"$job_details.label_name",
        "count": {"$sum": 1 }
      }
    }
])

The output is:

{
   "count": 2,
   "_id": [
   "shortlisted",
   "rejected"
    ]
}

But I want the output to be:

[
  { 
      "count": 1,  
      "_id": "shortlisted"  
  },  
  {  
      "count": 1,  
      "_id": "rejected"  
  }
]

How can I get this output?

like image 558
SuFi Avatar asked Oct 21 '16 04:10

SuFi


1 Answers

In unwind stage, field should be an array field. If not array field, it treats it as array of 1 element.

From the docs:

Changed in version 3.2: $unwind stage no longer errors on non-array operands. If the operand does not resolve to an array but is not missing, null, or an empty array, $unwind treats the operand as a single element array.


Answer to your query:

db.response.aggregate([
    {
        $project:
        {
            "job_details.label_name":1,
            _id:0
        }
    },
    {
        $unwind:"$job_details.label_name"
    },
    {
        $group:
        {
            _id:"$job_details.label_name",
            count:{$sum:1}
        }
    }
])

Refer Shell Output

like image 161
inaitgaJ Avatar answered Oct 17 '22 07:10

inaitgaJ