MongoDB从数组返回内部文档

时间:2019-09-05 07:18:10

标签: mongodb mongodb-query aggregation-framework projection

我正在尝试从文档中的数组中获取元素,而只有该元素我不需要整个文档

我尝试了另一种方法,但是它们都返回了整个文档

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"
     }
   ]

2 个答案:

答案 0 :(得分:0)

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)

答案 1 :(得分:0)

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

输入:

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