Sort documents by value in the last element of an array that matches filter. Mongodb

假装没事ソ 提交于 2021-01-28 11:24:17

问题


I have a collection of evaluationGroups with the following documents structure:

{
    "_id" : ObjectId("60073749694fd4d81e4d677d"),
    "AlertSettingId" : ObjectId("5ffddaaa0b1d2c30b191599a"),
    "CurrentStatus" : "success",
    "Evaluations" : [ 
        {
            "EvaluatedAt" : ISODate("2021-01-19T19:47:18.850Z"),
            "ReferenceValue" : 1.0,
            "Status" : "success"
        }, 
        {
            "EvaluatedAt" : ISODate("2021-01-19T19:52:16.423Z"),
            "ReferenceValue" : 1.0,
            "Status" : "triggered"
        }, 
        {
            "EvaluatedAt" : ISODate("2021-01-19T21:47:16.400Z"),
            "ReferenceValue" : 1.0,
            "Status" : "success"
        }
    ]
}
{
    "_id" : ObjectId("60085ec60a264ce3829a6335"),
    "AlertSettingId" : ObjectId("5ffddaaa0b1d2c30b191599a"),
    "CurrentStatus" : "triggered",
    "Evaluations" : [ 
        {
            "EvaluatedAt" : ISODate("2021-01-20T18:03:01.040Z"),
            "ReferenceValue" : 1.0,
            "Status" : "noDataFound"
        }, 
        {
            "EvaluatedAt" : ISODate("2021-01-20T22:04:43.983Z"),
            "ReferenceValue" : 1.0,
            "Status" : "triggered"
        }, 
        {
            "EvaluatedAt" : ISODate("2021-01-20T22:39:43.978Z"),
            "ReferenceValue" : 1.0,
            "Status" : "triggered"
        }, 
    ]
}
{
    "_id" : ObjectId("60099092f7386972de3e8a05"),
    "AlertSettingId" : ObjectId("5ffddaaa0b1d2c30b191599a"),
    "CurrentStatus" : "success",
    "Evaluations" : [ 
        {
            "EvaluatedAt" : ISODate("2021-01-21T14:32:48.697Z"),
            "ReferenceValue" : 1.0,
            "Status" : "noDataFound"
        }, 
        {
            "EvaluatedAt" : ISODate("2021-01-21T14:37:44.929Z"),
            "ReferenceValue" : 1.0,
            "Status" : "triggered"
        }, 
        {
            "EvaluatedAt" : ISODate("2021-01-21T14:42:44.928Z"),
            "ReferenceValue" : 1.0,
            "Status" : "triggered"
        }, 
        {
            "EvaluatedAt" : ISODate("2021-01-21T15:17:46.052Z"),
            "ReferenceValue" : 1.0,
            "Status" : "success"
        }
    ]
}

What I need to do is to sort all evaluation groups by the latest evaluation inside Evaluations (using EvaluatedAt property as the sort), but that the evaluation has also status triggered.

So, to sum up, I have to sort the groups, by the latest triggered Evaluation date.

I was looking at the question: Mongodb: sort documents by value in the last element of an array

And I liked this response of how to sort by last item, (because latest evaluations are at the end of the array in my case):

db.collection.aggregate([
    {
        $addFields: {
            lastSent: {
                $let: {
                    vars: {
                        last: {
                            $arrayElemAt: [ "$messages", -1 ]
                        }
                    },
                    in: "$$last.commData.sent.dateTime"
                }
            }
        }
    },
    { $sort: { lastSent: 1 } },
    { $project: { lastSent: 0 } }
])

But I would need to also filter evaluations by status "triggered" before getting the latest one.

How can achieve this using MongoDB aggregate query?


回答1:


You can use $filter operator,

  • $filter to filter Evaluations array on the base of Status
  • $max to get latest EvaluatedAt form filtered result
db.collection.aggregate([
  {
    $addFields: {
      lastSent: {
        $let: {
          vars: {
            filtered: {
              $filter: {
                input: "$Evaluations",
                cond: { $eq: ["$$this.Status", "triggered"] }
              }
            }
          },
          in: { $max: "$$filtered.EvaluatedAt" }
        }
      }
    }
  },
  { $sort: { lastSent: 1 } },
  { $project: { lastSent: 0 } }
])

Playground



来源:https://stackoverflow.com/questions/65831571/sort-documents-by-value-in-the-last-element-of-an-array-that-matches-filter-mon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!