Я ошеломлен.Я не могу найти выход в моей проблеме.У меня коллекция, как в чате.Это похоже на приватный чат.Итак, мне нужно сгруппировать сообщения по userX
, например:
{
userA : [arrayOfMessages(my and to me)],
userB: [arrayOfMessages(my and to me)]
}
Образцы документов
/* 1 */
{
"_id" : ObjectId("5b98deac801df913ec53c173"),
"group" : [],
"isRead" : false,
"recipient" : ObjectId("5b7175998a3b5705dc576e21"),
"message" : "asfsaf",
"sender" : ObjectId("5b69b678265fb633dc0ad165"),
"createdAt" : ISODate("2018-09-12T09:38:52.412Z"),
"updatedAt" : ISODate("2018-09-12T09:38:52.412Z"),
"__v" : 0
}
/* 2 */
{
"_id" : ObjectId("5b98df18431f682808087518"),
"group" : [],
"isRead" : false,
"recipient" : ObjectId("5b7175998a3b5705dc576e21"),
"message" : "SMS to teacher",
"sender" : ObjectId("5b69b678265fb633dc0ad165"),
"createdAt" : ISODate("2018-09-12T09:40:40.353Z"),
"updatedAt" : ISODate("2018-09-12T09:40:40.353Z"),
"__v" : 0
}
/* 3 */
{
"_id" : ObjectId("5b98e83d98cd47182c0c4bfd"),
"group" : [],
"isRead" : false,
"recipient" : ObjectId("5b7175998a3b5705dc576e21"),
"message" : "SMS to teachersaf",
"sender" : ObjectId("5b69b678265fb633dc0ad165"),
"createdAt" : ISODate("2018-09-12T10:19:41.310Z"),
"updatedAt" : ISODate("2018-09-12T10:19:41.310Z"),
"__v" : 0
}
/* 4 */
{
"_id" : ObjectId("5b990c377c315523fc18cce2"),
"isRead" : false,
"recipient" : ObjectId("5b8cdbb6e5ae71ec32a8051c"),
"message" : "1q",
"sender" : ObjectId("5b7175998a3b5705dc576e21"),
"createdAt" : ISODate("2018-09-12T12:53:11.214Z"),
"updatedAt" : ISODate("2018-09-12T12:53:11.214Z"),
"__v" : 0
}
/* 5 */
{
"_id" : ObjectId("5b990c457c315523fc18cce4"),
"isRead" : false,
"recipient" : ObjectId("5b8cdbb6e5ae71ec32a8051c"),
"message" : "1qa",
"sender" : ObjectId("5b7175998a3b5705dc576e21"),
"createdAt" : ISODate("2018-09-12T12:53:25.205Z"),
"updatedAt" : ISODate("2018-09-12T12:53:25.205Z"),
"__v" : 0
}
/* 6 */
{
"_id" : ObjectId("5b990c4a7c315523fc18cce6"),
"isRead" : false,
"recipient" : ObjectId("5b8cdbb6e5ae71ec32a8051c"),
"message" : "1qaz",
"sender" : ObjectId("5b7175998a3b5705dc576e21"),
"createdAt" : ISODate("2018-09-12T12:53:30.575Z"),
"updatedAt" : ISODate("2018-09-12T12:53:30.575Z"),
"__v" : 0
}
/* 7 */
{
"_id" : ObjectId("5b990c717c315523fc18cce8"),
"isRead" : false,
"recipient" : ObjectId("5b87cdfd3422340004b91ede"),
"message" : "1q",
"sender" : ObjectId("5b7175998a3b5705dc576e21"),
"createdAt" : ISODate("2018-09-12T12:54:09.213Z"),
"updatedAt" : ISODate("2018-09-12T12:54:09.213Z"),
"__v" : 0
}
/* 8 */
{
"_id" : ObjectId("5b990c757c315523fc18ccea"),
"isRead" : false,
"recipient" : ObjectId("5b87cdfd3422340004b91ede"),
"message" : "1qa",
"sender" : ObjectId("5b7175998a3b5705dc576e21"),
"createdAt" : ISODate("2018-09-12T12:54:13.274Z"),
"updatedAt" : ISODate("2018-09-12T12:54:13.274Z"),
"__v" : 0
}
/* 9 */
{
"_id" : ObjectId("5b990c787c315523fc18ccec"),
"isRead" : false,
"recipient" : ObjectId("5b87cdfd3422340004b91ede"),
"message" : "1qaz",
"sender" : ObjectId("5b7175998a3b5705dc576e21"),
"createdAt" : ISODate("2018-09-12T12:54:16.491Z"),
"updatedAt" : ISODate("2018-09-12T12:54:16.491Z"),
"__v" : 0
}
/* 10 */
{
"_id" : ObjectId("5b990ca87c315523fc18ccee"),
"isRead" : false,
"recipient" : ObjectId("5b75858eff41da0d3c6480ab"),
"message" : "1q",
"sender" : ObjectId("5b7175998a3b5705dc576e21"),
"createdAt" : ISODate("2018-09-12T12:55:04.620Z"),
"updatedAt" : ISODate("2018-09-12T12:55:04.620Z"),
"__v" : 0
}
/* 11 */
{
"_id" : ObjectId("5b990cac7c315523fc18ccf0"),
"isRead" : false,
"recipient" : ObjectId("5b75858eff41da0d3c6480ab"),
"message" : "1qa",
"sender" : ObjectId("5b7175998a3b5705dc576e21"),
"createdAt" : ISODate("2018-09-12T12:55:08.678Z"),
"updatedAt" : ISODate("2018-09-12T12:55:08.678Z"),
"__v" : 0
}
/* 12 */
{
"_id" : ObjectId("5b990cb07c315523fc18ccf2"),
"isRead" : false,
"recipient" : ObjectId("5b75858eff41da0d3c6480ab"),
"message" : "1qaz",
"sender" : ObjectId("5b7175998a3b5705dc576e21"),
"createdAt" : ISODate("2018-09-12T12:55:12.824Z"),
"updatedAt" : ISODate("2018-09-12T12:55:12.824Z"),
"__v" : 0
}
/* 13 */
{
"_id" : ObjectId("5b990d4c481c14080cc25d45"),
"isRead" : false,
"recipient" : ObjectId("5b69b678265fb633dc0ad165"),
"message" : "adadaddasd",
"sender" : ObjectId("5b7175998a3b5705dc576e21"),
"createdAt" : ISODate("2018-09-12T12:57:48.426Z"),
"updatedAt" : ISODate("2018-09-12T12:57:48.426Z"),
"__v" : 0
}
/* 14 */
{
"_id" : ObjectId("5b990d53481c14080cc25d47"),
"isRead" : false,
"recipient" : ObjectId("5b69b678265fb633dc0ad165"),
"message" : "adadaddasd asd a da",
"sender" : ObjectId("5b7175998a3b5705dc576e21"),
"createdAt" : ISODate("2018-09-12T12:57:55.369Z"),
"updatedAt" : ISODate("2018-09-12T12:57:55.369Z"),
"__v" : 0
}
/* 15 */
{
"_id" : ObjectId("5b990dcf481c14080cc25d4a"),
"isRead" : false,
"recipient" : ObjectId("5b7175998a3b5705dc576e21"),
"message" : "!!!!!!!!!!!!!",
"sender" : ObjectId("5b69b678265fb633dc0ad165"),
"createdAt" : ISODate("2018-09-12T12:59:59.610Z"),
"updatedAt" : ISODate("2018-09-12T12:59:59.610Z"),
"__v" : 0
}
/* 16 */
{
"_id" : ObjectId("5b990dd1481c14080cc25d4c"),
"isRead" : false,
"recipient" : ObjectId("5b7175998a3b5705dc576e21"),
"message" : "!!!!!!!!!!!!!",
"sender" : ObjectId("5b69b678265fb633dc0ad165"),
"createdAt" : ISODate("2018-09-12T13:00:01.679Z"),
"updatedAt" : ISODate("2018-09-12T13:00:01.679Z"),
"__v" : 0
}
Мне нужен совет.Может быть, мне стоит использовать агрегацию, но какую функцию агрегации мне следует использовать?