Search code examples
mongodbmongodb-queryaggregation-frameworkprojection

MongoDB Return Inner Document From Array


I am trying to fetch an element from an array in a document and only the element I don't want the entire document

I tried a different method but they all return the entire document

db.dept.find({"section.classes.CRN":"1901"}).limit(100)

db.dept.where("section.classes.CRN").eq("1901").limit(100)
json
{
    "_id" : ObjectId("5d70ab0c280d6b8ebb850cc1"),
    "name" : "Art Studio",
    "abbr" : "ARS",
    "section" : [
        {
            "type" : "Undergraduate Courses",
            "classes" : [
                {
                    "CRN" : "193",
                    "Course" : "ARS100",
                    "Sec" : "01",
                    "Title" : "Drawing I",
                    "Cr" : "3",
                    "Dates" : "8/26-12/19",
                    "Days" : "MR",
                    "Time" : "1230P-0320P",
                    "Loc" : "SAB 226",
                    "Instructor" : "Schuck",
                    "Attributes" : "",
                    "Avail" : "F"
                },
                {
                    "CRN" : "293",
                    "Course" : "ARS100",
                    "Sec" : "02",
                    "Title" : "Drawing I",
                    "Cr" : "3",
                    "Dates" : "8/26-12/19",
                    "Days" : "MR",
                    "Time" : "0330P-0620P",
                    "Loc" : "SAB 226",
                    "Instructor" : "Itty",
                    "Attributes" : "",
                    "Avail" : "F"
                },
                {...

I am trying to get this or something similar when searching for a set of CRN values

json
 [  {
    "CRN" : "193",
    "Course" : "ARS100",
    "Sec" : "01",
    "Title" : "Drawing I",
    "Cr" : "3",
    "Dates" : "8/26-12/19",
    ...

    "Instructor" : "Schuck",
    "Attributes" : "",
    "Avail" : "F"
     }
   ]

Solution

  • Try using the aggregate pipeline to project double nested array as:

    Input:

    [
      {
        "_id": ObjectId("5d70ab0c280d6b8ebb850cc1"),
        "name": "Art Studio",
        "abbr": "ARS",
        "section": [
          {
            "type": "Undergraduate Courses",
            "classes": [
              {
                "CRN": "193",
                "Course": "ARS100",
                "Sec": "01",
                "Title": "Drawing I",
                "Cr": "3",
                "Dates": "8/26-12/19",
                "Days": "MR",
                "Time": "1230P-0320P",
                "Loc": "SAB 226",
                "Instructor": "Schuck",
                "Attributes": "",
                "Avail": "F"
              },
              {
                "CRN": "293",
                "Course": "ARS100",
                "Sec": "02",
                "Title": "Drawing I",
                "Cr": "3",
                "Dates": "8/26-12/19",
                "Days": "MR",
                "Time": "0330P-0620P",
                "Loc": "SAB 226",
                "Instructor": "Itty",
                "Attributes": "",
                "Avail": "F"
              }
            ]
          }
        ]
      }
    ]
    

    Query: hereafter unwinding section you can filter classes for CRN

    db.collection.aggregate([
      {
        $unwind: "$section"
      },
      {
         $project: {
          name: 1,
          abbr: 1,
          "section.type": 1,
          "section.classes": {
           $filter: {
             input: "$section.classes",
             as: "item",
             cond: {
              $eq: [
                "$$item.CRN",
                "193"
              ]
            }
          }
      }
      }
    },
     {
       $group: {
         _id: "$_id",
         section: {
           $push: "$section"
         }
       }
      }
    ])
    

    Output: you can manage your keys as you want in project for adding new keys or replacing them.

     [
      {
        "_id": ObjectId("5d70ab0c280d6b8ebb850cc1"),
        "section": [
          {
            "classes": [
              {
                "Attributes": "",
                "Avail": "F",
                "CRN": "193",
                "Course": "ARS100",
                "Cr": "3",
                "Dates": "8/26-12/19",
                "Days": "MR",
                "Instructor": "Schuck",
                "Loc": "SAB 226",
                "Sec": "01",
                "Time": "1230P-0320P",
                "Title": "Drawing I"
              }
            ],
            "type": "Undergraduate Courses"
          }
        ]
      }
    ]