mongodb - 如何在 mongodb 中使用 facet 操作查找字段的不同值

标签 mongodb mongodb-query aggregation-framework aggregation facet

filteredAccording 部分和 categorizedBy 使用我在链接中提供的查询按预期工作,但我在 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"
      ]
    }​​​​​​​​
  ]

任何帮助将不胜感激!!

最佳答案

快速修复您的查询,

大学:

  • $addFields,移除$project,只为isMarked添加一个操作
  • $unwind 解构dept数组
  • $group by dept 并获取根的值数组

查找区别:

  • $group 为 null 并得到唯一的 courses 数组和 status
  • $reduce 迭代 Courses 嵌套数组的循环并使用 $setUnion
  • 获取唯一数组
  • dest字段中制作类(class)和status数组
  • $unwind 解构dest数组
  • $replaceRootdest 对象替换为 root
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" } }
      ]
    }
  }
])

Playground

关于mongodb - 如何在 mongodb 中使用 facet 操作查找字段的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66167965/

相关文章:

mongodb - mongodb - map 减少和查找

ruby - 按条件订阅 channel

node.js - 仅当 mongodb 中的 operationType 为 'update' 时才过滤

mongodb - 如何将聚合结果写入数据库

MongoDB $ifNull 条件与 mgo

java - Spring Data MongoDB : MergeOperation returns the whole collection. 为什么?

子文档键上的 MongoDB 索引

mongodb - 如何使用mongo聚合计算不同文档值之间的差异?

mongodb - 使用 go-gin 和 mgo 从 mongoDB 通过 id 获取民意调查时出错

node.js - Mongoose 通过 ObjectId 查找