I need to obtain the last message of a collection of messages based on the conversation room, for this I need to obtain only the last element of an array that is previously ordered by date from highest to lowest
(This to get the most recent message from that chat).
The result I get is that it brings me all the messages and not just one
I need get this:
{
"_id":"ObjectId(""5fff8086d1f75d0313fd20d5"")",
"usuarios":[
{
"id":"ObjectId(""5fff7521d1f75d0313fd20d4"")",
"nombre_usuario":"dagoberticohd",
"foto_url":"http://res."
},
{
"id":"ObjectId(""5fff7521d1f75d0313fd20d4"")",
"nombre_usuario":"dagoberticohd",
"foto_url":"http://res."
}
],
"mensaje":{
"_id":"ObjectId(""5fff80b2d1f75d0313fd20d7"")",
"text":"Ghv",
"createdAt":"2021-01-21T23:22:26.855150",
"autor":"ObjectId(""5fff7521d1f75d0313fd20d4"")",
"room":"ObjectId(""5fff8086d1f75d0313fd20d5"")",
"received":true
},
"deleted_by":[
{
"_id":"ObjectId(""5ffb8107e3d065e99e0949bd"")",
"delete_date":"2021-01-18T23:21:42.580627"
}
]
}
I get this:
{
"_id":"ObjectId(""5fff8086d1f75d0313fd20d5"")",
"usuarios":[
{
"id":"ObjectId(""5fff7521d1f75d0313fd20d4"")",
"nombre_usuario":"dagoberticohd",
"foto_url":"http://res."
},
{
"id":"ObjectId(""5fff7521d1f75d0313fd20d4"")",
"nombre_usuario":"dagoberticohd",
"foto_url":"http://res."
}
],
"mensaje":{
"_id":"ObjectId(""5fff80b2d1f75d0313fd20d7"")",
"text":"Ghv",
"createdAt":"2021-01-21T23:22:26.855150",
"autor":"ObjectId(""5fff7521d1f75d0313fd20d4"")",
"room":"ObjectId(""5fff8086d1f75d0313fd20d5"")",
"received":true
},
"deleted_by":[
{
"_id":"ObjectId(""5ffb8107e3d065e99e0949bd"")",
"delete_date":"2021-01-18T23:21:42.580627"
}
]
}{
"_id":"ObjectId(""5fff8086d1f75d0313fd20d5"")",
"usuarios":[
{
"id":"ObjectId(""5fff7521d1f75d0313fd20d4"")",
"nombre_usuario":"dagoberticohd",
"foto_url":"http://res."
},
{
"id":"ObjectId(""5fff7521d1f75d0313fd20d4"")",
"nombre_usuario":"dagoberticohd",
"foto_url":"http://res."
}
],
"mensaje":{
"_id":"ObjectId(""600cd16b3de98712caa8b7dc"")",
"text":"Xx",
"createdAt":"2021-01-20T01:46:19.377185",
"autor":"ObjectId(""5ffb8107e3d065e99e0949bd"")",
"room":"ObjectId(""5fff8086d1f75d0313fd20d5"")",
"received":false
},
"deleted_by":[
{
"_id":"ObjectId(""5ffb8107e3d065e99e0949bd"")",
"delete_date":"2021-01-18T23:21:42.580627"
}
]
}
My code:
data_chats = rooms_db.aggregate([
{
'$match': {
'usuarios': ObjectId(usuario['_id']),
}
},
{
'$lookup': {
'from': 'mensajes',
'localField':'mensajes',
'foreignField': '_id',
'as': 'mensaje'
}
},
{
'$lookup': {
'from': 'usuarios',
'localField':'usuarios',
'foreignField': '_id',
'as': 'usuario'
}
},
{"$unwind": "$usuario"},
{"$unwind": "$usuario._id"},
{"$match": {"usuario._id": {"$nin": [ObjectId(usuario['_id'])]}}},
{"$unwind": "$mensaje"},
{
'$sort': {'mensaje.createdAt': -1}
},
{
'$project': {
'mensaje': '$mensaje',
'usuarios': {
'id': '$usuario._id',
'nombre_usuario': '$usuario.nombre_usuario',
'foto_url': '$usuario.foto_url'
},
'deleted_by': {
'$filter': {
'input': "$deleted_by",
'as': "deleted_convers",
'cond': { '$eq': [ "$$deleted_convers._id", ObjectId(usuario['_id'])] }
}
}
}
},
])
question from:
https://stackoverflow.com/questions/65892278/get-last-element-of-array-but-before-sorted-by-date