我知道这不是最近的问题之一,但我有不同的解决方案。
带有一个投影的选项(较慢):
{$project: {
version: {
major: { $arrayElemAt: [{ $map: { input: { $split: ["$metadata.appVersion", "."] }, as: "vn", in: { $toInt: "$$vn" } } }, 0] },
minor: { $arrayElemAt: [{ $map: { input: { $split: ["$metadata.appVersion", "."] }, as: "vn", in: { $toInt: "$$vn" } } }, 1] },
patch: { $arrayElemAt: [{ $map: { input: { $split: ["$metadata.appVersion", "."] }, as: "vn", in: { $toInt: "$$vn" } } }, 2] }
}
}
具有两个预测的选项(需要汇总):
{
$project: {
versionAsArray: {
$map: {
input: {
$split: ["$metadata.appVersion", "."]
},
as: "vn",
in: { $toInt: "$$vn" }
}
}
}
},
{
$project: {
version: {
version: {
major: { $arrayElemAt: ["$versionAsArray", 0] },
minor: { $arrayElemAt: ["$versionAsArray", 1] },
patch: { $arrayElemAt: ["$versionAsArray", 2] },
}
}
}
}
最后你可以正常使用$sort: { version: 1 }