我尝试对mongoDB集合中(使用nodeJS驱动程序)多个字段的数组中的唯一值进行排序。
一个小型数据集:
聚合的管道:
一个小型数据集:
[{
"_id" : "5c93db3dd0184516406013f7",
"filters" : {
"genres" : [
{
"_id" : "9CXBYc4qP8sqcNMZ5",
"fr" : "Art Abstrait",
"en" : "Abstract Art",
"de" : "Abstrakte Kunst",
"it" : "Arte astratta",
"es" : "Arte Abstracto"
}
],
"subjects" : [
{
"_id" : "3QjL6YSfmuY6NFHGG",
"fr" : "Abstrait",
"en" : "Abstract",
"de" : "Abstrakt",
"it" : "Astratto",
"es" : "Abstracto"
}
],
"type" : {
"_id" : "CYK2WcepkJsy5xXMo",
"fr" : "Gravure au carborundum",
"en" : "Carborundum etching",
"de" : "Carborundum Radierung",
"it" : "Incisione carborandum",
"es" : "Grabado al Carborundum"
}
}
},
{
"_id" : "5c93db3ed0184516406013f8",
"filters" : {
"genres" : [
{
"_id" : "9CXBYc4qP8sqcNMZ5",
"fr" : "Art Abstrait",
"en" : "Abstract Art",
"de" : "Abstrakte Kunst",
"it" : "Arte astratta",
"es" : "Arte Abstracto"
}
],
"subjects" : [
{
"_id" : "3QjL6YSfmuY6NFHGG",
"fr" : "Abstrait",
"en" : "Abstract",
"de" : "Abstrakt",
"it" : "Astratto",
"es" : "Abstracto"
}
],
"type" : {
"_id" : "CYK2WcepkJsy5xXMo",
"fr" : "Gravure au carborundum",
"en" : "Carborundum etching",
"de" : "Carborundum Radierung",
"it" : "Incisione carborandum",
"es" : "Grabado al Carborundum"
}
}
},
{
"_id" : "5c93e19ed018451640601da6",
"filters" : {
"genres" : [
{
"_id" : "9CXBYc4qP8sqcNMZ5",
"fr" : "Art Abstrait",
"en" : "Abstract Art",
"de" : "Abstrakte Kunst",
"it" : "Arte astratta",
"es" : "Arte Abstracto"
}
],
"subjects" : [
{
"_id" : "3QjL6YSfmuY6NFHGG",
"fr" : "Abstrait",
"en" : "Abstract",
"de" : "Abstrakt",
"it" : "Astratto",
"es" : "Abstracto"
}
],
"type" : {
"_id" : "KfGWEHL2pAto8nfze",
"fr" : "Gravure",
"en" : "Etching",
"de" : "Radierung",
"it" : "Incisione",
"es" : "Grabado"
}
}
}]
我的查询结果(使用lang = 'en'
):
{
"subjects": [
{
"_id": "3QjL6YSfmuY6NFHGG",
"fr": "Abstrait",
"en": "Abstract",
"de": "Abstrakt",
"it": "Astratto",
"es": "Abstracto"
},
{
"_id": "3QjL6YSfmuY6NFHGG",
"fr": "Abstrait",
"en": "Abstract",
"de": "Abstrakt",
"it": "Astratto",
"es": "Abstracto"
}
],
"genres": [
{
"_id": "9CXBYc4qP8sqcNMZ5",
"fr": "Art Abstrait",
"en": "Abstract Art",
"de": "Abstrakte Kunst",
"it": "Arte astratta",
"es": "Arte Abstracto"
},
{
"_id": "9CXBYc4qP8sqcNMZ5",
"fr": "Art Abstrait",
"en": "Abstract Art",
"de": "Abstrakte Kunst",
"it": "Arte astratta",
"es": "Arte Abstracto"
}
],
"types": [
{
"_id": "CYK2WcepkJsy5xXMo",
"fr": "Gravure au carborundum",
"en": "Carborundum etching",
"de": "Carborundum Radierung",
"it": "Incisione carborandum",
"es": "Grabado al Carborundum"
},
{
"_id": "KfGWEHL2pAto8nfze",
"fr": "Gravure",
"en": "Etching",
"de": "Radierung",
"it": "Incisione",
"es": "Grabado"
}
]
}
聚合的管道:
[
{ $unwind: '$filters.subjects' },
{ $unwind: '$filters.genres' },
{ $group: {
_id: null,
subjects: { $addToSet: '$filters.subjects' },
types: { $addToSet: '$filters.type' },
genres: { $addToSet: '$filters.genres' },
}},
{ $unwind: '$subjects' },
{ $unwind: '$genres' },
{ $unwind: '$types' },
{ $sort: {
[`subjects.${lang}`]: 1,
[`types.${lang}`]: 1,
[`genres.${lang}`]: 1,
}},
{ $group: {
_id: null,
subjects: { $push: '$subjects' },
types: { $push: '$types' },
genres: { $push: '$genres' },
}},
{ $project: {
_id: false,
subjects: '$subjects',
types: '$types',
genres: '$genres'
}}
]
与其获得唯一值的已排序数组:
[A, B, C, D, ...]
我获得了带有非唯一值的已排序数组:
[A, A, A, B, B, B, C, C, C, D, D, D, ...]
这使得$addToSet
分组无用。
你知道我错在哪里吗?