我正在尝试编写一个查询,该查询将以升序对 special_dates
数组进行排序,然后根据 MongoDB 中数组中的第一个日期进行排序。
我有以下数据:
/* 1 */
{
"_id" : ObjectId("5f9c0dfaf88d0a318ad826b1"),
"holiday_for" : "range",
"specific_dates" : [
"2020-10-30",
"2020-10-31",
"2020-11-01",
"2020-11-02",
"2020-11-03",
"2020-11-04",
"2020-11-05",
"2020-11-06",
"2020-11-07",
"2020-11-08",
"2020-11-09",
"2020-11-10",
"2020-11-11",
"2020-11-12",
"2020-11-13",
"2020-11-14",
"2020-11-15",
"2020-11-16",
"2020-11-17",
"2020-11-18",
"2020-11-19",
"2020-11-20",
"2020-11-21",
"2020-11-22",
"2020-11-23",
"2020-11-24",
"2020-11-25",
"2020-11-26",
"2020-11-27",
"2020-11-28",
"2020-11-29",
"2020-11-30",
"2020-12-01",
"2020-12-02",
"2020-12-03",
"2020-12-04",
"2020-12-05",
"2020-12-06",
"2020-12-07",
"2020-12-08",
"2020-12-09",
"2020-12-10",
"2020-12-11",
"2020-12-12",
"2020-12-13",
"2020-12-14",
"2020-12-15",
"2020-12-16",
"2020-12-17",
"2020-12-18",
"2020-12-19",
"2020-12-20",
"2020-12-21",
"2020-12-22",
"2020-12-23",
"2020-12-24",
"2020-12-25"
],
"description" : "Add holiday",
"location_id" : 0
}
/* 2 */
{
"_id" : ObjectId("5fa2220abb17c6473c2f4368"),
"holiday_for" : "specific",
"specific_dates" : [
"2020-10-31",
"2020-11-02",
"2020-10-30"
],
"description" : "Add holiday",
"location_id" : 0
}
/* 3 */
{
"_id" : ObjectId("5fc7591a3b1b0175500d07ea"),
"holiday_for" : "specific",
"specific_dates" : [
"2020-12-05",
"2020-12-02",
"2020-12-01"
],
"location_id" : 0
}
我需要根据 special_dates
数组中的第一个日期对这些文档进行排序:
db.getCollection('holidays').aggregate([
{$unwind: "$specific_dates"},
{$sort: {"specific_dates": -1}},
{$group: {
"_id": "$_id",
"holiday_for": {$first: "$holiday_for"},
"description": {$first: "$description"},
"location_id": {$first: "$location_id"},
"specific_dates": {$push: "$specific_dates"}}},
])
但这给出了以下意想不到的结果。有人可以指导我如何执行此操作吗?
最佳答案
无需$unwind
,您可以使用$first
从数组中获取第一个元素,
db.collection.aggregate([
{ $addFields: { first_date: { $first: "$specific_dates" } } },
{ $sort: { first_date: -1 } }
])
第二个选项您可以使用$arrayElemAt
,
db.collection.aggregate([
{ $addFields: { first_date: { $arrayElemAt: ["$specific_dates", 0] } } },
{ $sort: { first_date: -1 } }
])
根据您的评论的第三个选项,首先对该数组进行排序,
$reduce
迭代使用$setUnion
、$concatArrays
按升序排序的special_dates
数组的循环> 第一个当前对象和第二个初始值,它将改变数组的顺序为降序- 接下来的两个阶段保持不变
db.collection.aggregate([
{
$addFields: {
specific_dates: {
$reduce: {
input: { $setUnion: "$specific_dates" },
initialValue: [],
in: { $concatArrays: [["$$this"], "$$value"] }
}
}
}
},
{ $addFields: { first_date: { $arrayElemAt: ["$specific_dates", 0] } } },
{ $sort: { first_date: -1 } }
])
关于mongodb - 如何在 MongoDB 中对数组类型的字段进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65109264/