MongoDB Return 来自数组的内部文档

MongoDB Return Inner Document From Array

我正在尝试从文档中的数组中获取一个元素,但我不想获取整个文档中的元素

我尝试了不同的方法,但他们都return整个文档

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"
                },
                {...

我在搜索一组 CRN 值时试图得到这个或类似的东西

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

    "Instructor" : "Schuck",
    "Attributes" : "",
    "Avail" : "F"
     }
   ]
db.dept.find({"section.classes.CRN":"1901"},{"section.classes":1}).limit(100)

它在 mongodb 中称为 projection,您在查找查询中传递第二个对象以指定结果中需要的字段。

所以根据你上面的情况,如果你想要名字,结果中的部分你应该传递这样的东西

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

尝试使用聚合管道将双嵌套数组投影为:

输入:

[
  {
    "_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"
          }
        ]
      }
    ]
  }
]

查询: 以下展开部分您可以过滤 类 以获得 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"
     }
   }
  }
])

输出: 您可以根据需要在项目中管理您的密钥,以添加新密钥或替换它们。

 [
  {
    "_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"
      }
    ]
  }
]