Node.js Mongodb:按数组最后一个元素中的值对文档进行排序
我有一个集合“collectionName”,其中包含许多类似以下的文档:Node.js Mongodb:按数组最后一个元素中的值对文档进行排序,node.js,mongodb,sorting,mongoose,mongodb-query,Node.js,Mongodb,Sorting,Mongoose,Mongodb Query,我有一个集合“collectionName”,其中包含许多类似以下的文档: { "_id" : ObjectId("5f072d45856bc306147c1dcd"), "readOnly" : false, "participants" : [ { "clientType" : "support",
{
"_id" : ObjectId("5f072d45856bc306147c1dcd"),
"readOnly" : false,
"participants" : [
{
"clientType" : "support",
"docId" : "",
"metaData" : {
"fullName" : "Support",
"email" : "Support@mydomain.com",
"phoneNum" : "+1234567890"
}
},
{
"clientType" : "worker",
"docId" : "5e21c48ee178473be81e032e",
"metaData" : {
"fullName" : "Rami",
"email" : "myemail@gmail.com",
"phoneNum" : "+1234567890"
}
}
],
"messages" : [
{
"id" : "IGK-fIt-2zz",
"sender" : "support, ",
"body" : "CONTROL: Agent \"Greendizeriii\" has answered the conversation",
"commData" : {
"sent" : {
"state" : true,
"dateTime" : ISODate("2020-07-11T06:33:48.348Z")
},
"delivered" : {
"state" : false
},
"seen" : {
"state" : true,
"dateTime" : ISODate("2020-07-11T06:40:06.851Z")
}
}
},
{
"id" : "L0f-hwj-QUL",
"sender" : "support, ",
"body" : "Hello rami, How can I help you today",
"commData" : {
"sent" : {
"state" : true,
"dateTime" : ISODate("2020-07-11T06:35:57.406Z")
},
"delivered" : {
"state" : false
},
"seen" : {
"state" : true,
"dateTime" : ISODate("2020-07-11T06:40:06.851Z")
}
}
},
{
"id" : "n0k-s3u-UmN",
"sender" : "support, ",
"body" : "are you still there ?",
"commData" : {
"sent" : {
"state" : true,
"dateTime" : ISODate("2020-07-11T06:36:13.350Z")
},
"delivered" : {
"state" : false
},
"seen" : {
"state" : true,
"dateTime" : ISODate("2020-07-11T06:40:06.851Z")
}
}
}
],
"controlData" : {
"isReserved" : {
"state" : false,
"reservedUntil" : null,
"operatorId" : null
}
},
"__v" : 0
},
{
"_id" : ObjectId("5f1f143f4134643044638902"),
"readOnly" : false,
"participants" : [
{
"clientType" : "support",
"docId" : "",
"metaData" : {
"fullName" : "Support",
"email" : "Support@mydomain.com",
"phoneNum" : "+1234567890"
}
},
{
"clientType" : "worker",
"docId" : "5ea9409ff3243f483c2483ed",
"metaData" : {
"fullName" : "Ronaldo",
"email" : "dummyemail02@gmail.com",
"phoneNum" : "+1234567890"
}
}
],
"messages" : [
{
"id" : "uto-qQb-0cr",
"sender" : "support, ",
"body" : "CONTROL: Agent \"Greendizeriii\" has answered the conversation",
"commData" : {
"sent" : {
"state" : true,
"dateTime" : ISODate("2020-07-27T17:51:59.753Z")
},
"delivered" : {
"state" : false
},
"seen" : {
"state" : true,
"dateTime" : ISODate("2020-07-27T17:52:01.240Z")
}
}
},
{
"id" : "FQg-fSQ-jQ1",
"sender" : "support, ",
"body" : "jjj",
"commData" : {
"sent" : {
"state" : true,
"dateTime" : ISODate("2020-07-27T17:51:59.751Z")
},
"delivered" : {
"state" : false
},
"seen" : {
"state" : true,
"dateTime" : ISODate("2020-07-27T17:52:01.240Z")
}
}
},
{
"id" : "Uqm-J4S-LUT",
"sender" : "support, ",
"body" : "CONTROL: Agent \"Greendizeriii\" has left the conversation",
"commData" : {
"sent" : {
"state" : true,
"dateTime" : ISODate("2020-07-27T17:53:05.142Z")
},
"delivered" : {
"state" : false
},
"seen" : {
"state" : true,
"dateTime" : ISODate("2020-07-27T17:53:05.558Z")
}
}
},
{
"id" : "gs1-Qqz-jkI",
"sender" : "worker, 5ea9409ff3243f483c2483ed",
"body" : "Hello",
"commData" : {
"sent" : {
"state" : true,
"dateTime" : ISODate("2020-07-25T03:58:50.600Z")
},
"delivered" : {
"state" : false
},
"seen" : {
"state" : false
}
}
}
],
"controlData" : {
"isReserved" : {
"state" : false,
"reservedUntil" : null,
"operatorId" : null
}
},
"__v" : 0
}
例如,这里有两个文档
每个字段都有消息字段,该字段是元素/对象的数组
我想使用messages数组的最新元素(commData.sent.dateTime
)对它们进行排序
我知道我可以做一些事情,比如:
db.getCollection('collectionName').find({}).sort({"messages.0.commData.sent.dateTime": 1})
它将返回使用messages数组中第一个元素的commData.sent.dateTime排序的文档。但我不知道在这种特殊情况下如何使用数组中的最后一个元素进行排序
注意:
问题的答案是:
在我的情况下将不起作用,因为我需要用于对文档进行排序的字段(dateTime)位于嵌套对象内
非常感谢你的帮助
谢谢您可以用它来计算上次发送日期。将-1
作为表示数组最后一项的参数:
db.collection.aggregate([
{
$addFields: {
lastSent: {
$let: {
vars: {
last: {
$arrayElemAt: [ "$messages", -1 ]
}
},
in: "$$last.commData.sent.dateTime"
}
}
}
},
{ $sort: { lastSent: 1 } },
{ $project: { lastSent: 0 } }
])
这是否回答了您的问题?嘿@Gibbs,你建议的答案在我的例子中不起作用,因为我需要用来对文档进行排序的字段(dateTime)位于嵌套对象中。非常感谢@mickl@mickl,如果我想得到与特定过滤器匹配的最后一个对象。我怎么用这个?例如,具有特定“发件人”的最后一封邮件。@letie请打开单独的邮件question@mickl我提出了这个问题: