如何在MongoDB中对数组类型的字段进行排序
我正在尝试编写一个查询,它将按升序对如何在MongoDB中对数组类型的字段进行排序,mongodb,Mongodb,我正在尝试编写一个查询,它将按升序对特定的\u日期数组进行排序,然后根据MongoDB中数组中的第一个日期进行排序 我有以下资料: /* 1 */ { "_id" : ObjectId("5f9c0dfaf88d0a318ad826b1"), "holiday_for" : "range", "specific_dates" : [ "2020-
特定的\u日期
数组进行排序,然后根据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
}
我需要根据specific_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
特定日期数组的循环,
第一个当前对象和第二个初始值,它将数组的顺序更改为降序$concatarray
- 接下来的两个阶段保持不变
按数组字段排序(首先按升序对日期数组排序,然后选择要应用排序的第一个日期)
此查询既不适用于mongodb v4.2,也不适用于Robomongo v1.3。它说,
无法识别的表达式“$first”
好的$arrayElemAt
现在可以正常工作了。你能不能再给我一个提示,让我先对日期数组排序,然后再选择第一个日期?你想把那个日期数组缩短吗?为此,您可以使用$unwind、sort和$group,第二个选项可以使用$funciton运算符。或者我只是在回答中添加了另一个选项。感谢您在回答中添加了第三个选项。但是我尝试了另一个,它给了我想要的结果:请告诉我你对它的评价。在某些情况下它会崩溃吗?$first
在4.4中是新的,
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 } }
])
db.getCollection('holidays').aggregate([
{
$project: {
"_id": 1,
"holiday_for": 1,
"description": 1,
"location_id": 1,
"specific_dates": 1,
"first_date": {$min: "$specific_dates"}
}
},
{
$sort: {
first_date: -1
}
}
])