如何在 mongodb 中使用分面操作查找字段的不同值
How to find distinct values of fields using facet operation in mongodb
filteredAccording 部分和 categorizedBy 使用我在 link 中提供的查询按预期工作,但我在 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"
]
}
]
我用 link 尝试过,但没有收到预期的响应。
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
并获取 root 的值数组
findDistinct:
$group
by null 并得到唯一的 courses
数组和 status
$reduce
迭代 Courses
嵌套数组的循环并使用 $setUnion
获得唯一数组
- 在
dest
字段中 status
当然制作数组
$unwind
解构dest
数组
$replaceRoot
将 dest
对象替换为根目录
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" } }
]
}
}
])
filteredAccording 部分和 categorizedBy 使用我在 link 中提供的查询按预期工作,但我在 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"
]
}
]
我用 link 尝试过,但没有收到预期的响应。 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
bydept
并获取 root 的值数组
findDistinct:
$group
by null 并得到唯一的courses
数组和status
$reduce
迭代Courses
嵌套数组的循环并使用$setUnion
获得唯一数组
- 在
dest
字段中status
当然制作数组 $unwind
解构dest
数组$replaceRoot
将dest
对象替换为根目录
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" } }
]
}
}
])