Answer the question
In order to leave comments, you need to log in
Recent chat messages?
We have an array with a list of chat ids - [1, 2]
Task: to extract one last message in each chat from the messages collection.
The message collection is shown below.
created_at - timestamp time
[
{
chat_id: 1,
text: '123',
created_at: 1
},
{
chat_id: 1,
text: '123',
created_at: 2
},
{
chat_id: 1,
text: '123',
created_at: 3
},
{
chat_id: 2,
text: 'asd123',
created_at: 1
},
{
chat_id: 2,
text: 'asd123',
created_at: 3
},
{
chat_id: 5,
text: '123312',
created_at: 1
}
]
db.message.find({chat_id: {$in: [1, 2]}}, {}, {
sort: {created_at: -1},
limit: 1
});
[
{
chat_id: 1,
text: '123',
created_at: 3
},
{
chat_id: 2,
text: 'asd123',
created_at: 3
}
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question