首页 > 解决方案 > 如何在 mongodb 中使用 facet 操作查找字段的不同值

问题描述

使用我在链接中提供的查询,filteredAccording 部分和分类依据按预期工作,但我在 findDistinct 部分遇到问题。

在 mongodb 我有以下数据:

 {
        "_id": 10001,
        "university": "SPYU",
        "Courses": [
          "English",
          "French"
        ],
        "dept": [
          "Literature"
        ],
        "type": [
          "Autonomous"
        ],
        "status": "ACTIVE",
        "isMarked": true
      },
      {
        "_id": 10002,
        "university": "SPYU",
        "Courses": [
          "English",
          "French"
        ],
        "dept": [
          "Literature"
        ],
        "type": [
          "Autonomous"
        ],
        "status": "NON-ACTIVE",
        "isMarked": true
      }

我希望得到的回应是:

 "university": [
  {
    "name": "Literature",
    "values": [
      {
        "_id": 10001,
        "university": "SPYU",
        "Courses": [
          "English",
          "French"
        ],
        "dept": [
          "Literature"
        ],
        "type": [
          "Autonomous"
        ],
        "status": "ACTIVE",
        "isMarked": true
      },
      {
        "_id": 10002,
        "university": "SPYU",
        "Courses": [
          "English",
          "French"
        ],
        "dept": [
          "Literature"
        ],
        "type": [
          "Autonomous"
        ],
        "status": "NON-ACTIVE",
        "isMarked": true
      }
    ]
  }
],
 "findDistinct": [
    {​​​​​​​​
      "name": "Courses",
      "values": [
        "English",
         "French"
      ]
    }​​​​​​​​,
    {​​​​​​​​
      "name": "Status",
      "values": [
        "ACTIVE",
        "NON-ACTIVE"
      ]
    }​​​​​​​​
  ]

我使用此链接进行了尝试,但响应未按预期进行。 https://mongoplayground.net/p/XECZvRMmt3T

现在,回应是这样的

 "universities": [
  {
    "name": "Literature",
    "values": [
      {
        "_id": 10001,
        "university": "SPYU",
        "Courses": [
          "English",
          "French"
        ],
        "dept": [
          "Literature"
        ],
        "type": [
          "Autonomous"
        ],
        "status": "ACTIVE",
        "isMarked": true
      },
      {
        "_id": 10002,
        "university": "SPYU",
        "Courses": [
          "English",
          "French"
        ],
        "dept": [
          "Literature"
        ],
        "type": [
          "Autonomous"
        ],
        "status": "NON-ACTIVE",
        "isMarked": true
      }
    ]
  }
],
"findDistinct": [
    {​​​​​​​​
      "Courses": [
        "English",
         "French"
      ]
    }​​​​​​​​,
    {​​​​​​​​
      "status": [
        "ACTIVE",
        "NON-ACTIVE"
      ]
    }​​​​​​​​
  ]

任何帮助将不胜感激!!

标签: mongodbmongodb-queryaggregation-frameworkaggregationfacet

解决方案


快速修复您的查询,

大学:

  • $addFields, 删除 $project 并仅添加一项操作isMarked
  • $unwind解构dept数组
  • $group通过dept并获取根的值数组

发现区别:

  • $group通过 null 并获得唯一的courses数组和status
  • $reduceCourses迭代嵌套数组的循环并使用获取唯一数组$setUnion
  • status制作课程和dest现场数组
  • $unwind解构dest数组
  • $replaceRootdest对象替换为根
db.collection.aggregate([
  { $match: { university: "SPYU" }
  },
  {
    $facet: {
      universities: [
        { $addFields: { isMarked: { $in: ["French", "$Courses"] } } },
        { $unwind: "$dept" },
        {
          $group: {
            _id: "$dept",
            values: { $push: "$$ROOT" }
          }
        }
      ],
      findDistinct: [
        {
          $group: {
            _id: null,
            Courses: { $addToSet: "$Courses" },
            Status: { $addToSet: "$status" }
          }
        },
        {
          $project: {
            _id: 0,
            dist: [
              {
                name: "Courses",
                values: {
                  $reduce: {
                    input: "$Courses",
                    initialValue: [],
                    in: { $setUnion: ["$$this", "$$value"] }
                  }
                }
              },
              {
                name: "Status",
                values: "$Status"
              }
            ]
          }
        },
        { $unwind: "$dist" },
        { $replaceRoot: { newRoot: "$dist" } }
      ]
    }
  }
])

操场


推荐阅读