has a collection of messages:
{
"date": NumberLong(1421134514),
"sender": "53172480f9cd0e682840b9f7",
"recipient": "52f37fbaf9cd0e02773c97b1",
"isRead": false,
"_id": "54b4cab2f6a48ce34f8b5a75",
"text": "Hello!"
},
{
"date": NumberLong(1421135561),
"sender": "53172480f9cd0e682840b9f7",
"recipient": "52f37fbaf9cd0e02773c97b1",
"isRead": false,
"_id": "54b4cec9f6a48ce34f8b6429",
"text": "Hello 2!"
},
{
"date": NumberLong(1421135618),
"sender": "53072122f9cd0ee76306dc5a",
"recipient": "52f37fbaf9cd0e02773c97b1",
"isRead": false,
"_id": "54b4cf02f6a48ce54f8b62f9",
"text": "Hello 3!"
},
{
"date": NumberLong(1421136457),
"sender": "52f37fbaf9cd0e02773c97b1",
"recipient": "52ea178ff9cd0e9f24d776b4",
"isRead": false,
"_id": "54b4d249f6a48ce54f8b6b9f"
"text": "Hello 4!"
}
It is necessary to choose the latter dialogues, where the current user is either the sender or recipient.
For example, for a user with ID = '52f37fbaf9cd0e02773c97b1', should get 3 records.
It turns out to build two separate queries:
$result = \DB::$connection->message->aggregate(array(
array('$match' => array('sender' => \Core::getModule('users')->user->_id)),
array('$group' => array('_id' => '$recipient')),
));
$result2 = \DB::$connection->message->aggregate(array(
array('$match' => array('recipient' => \Core::getModule('users')->user->_id)),
array('$group' => array('_id' => '$sender')),
));
Is it possible to somehow combine these two queries into one and sort the records by date?
What you need here is a "unique key" value generated for each combination of "sender and recipient". If you want to do this on a regular basis then I would suggest storing the value in the document. But this is a way you can get out of the problem using the aggregation framework:
db.messages.aggregate([
{ "$project": {
"combined": { "$map": {
"input": { "$literal": ["A","B"] },
"as": "bin",
"in": { "$cond": [
{ "$eq": [ "$$bin", "A" ] },
"$sender",
"$recipient"
]}
}},
"doc": "$$ROOT"
}},
{ "$unwind": "$combined" },
{ "$sort": { "_id": 1, "combined": 1, "doc.date": -1 } },
{ "$group": {
"_id": "$_id",
"combined": { "$push": "$combined" },
"doc": { "$first": "$doc" }
}},
{ "$group": {
"_id": "$combined",
"doc": { "$first": "$doc" }
}}
])
That narrows down your sample to the "unique" 3 combinations on "sender / recipient" and returns the last document in the conversation between that "pair":
{
"_id" : [
"52f37fbaf9cd0e02773c97b1",
"53172480f9cd0e682840b9f7"
],
"doc" : {
"_id" : "54b4cec9f6a48ce34f8b6429",
"date" : NumberLong(1421135561),
"sender" : "53172480f9cd0e682840b9f7",
"recipient" : "52f37fbaf9cd0e02773c97b1",
"isRead" : false,
"text" : "Hello 2!"
}
}
{
"_id" : [
"52f37fbaf9cd0e02773c97b1",
"53072122f9cd0ee76306dc5a"
],
"doc" : {
"_id" : "54b4cf02f6a48ce54f8b62f9",
"date" : NumberLong(1421135618),
"sender" : "53072122f9cd0ee76306dc5a",
"recipient" : "52f37fbaf9cd0e02773c97b1",
"isRead" : false,
"text" : "Hello 3!"
}
}
{
"_id" : [
"52ea178ff9cd0e9f24d776b4",
"52f37fbaf9cd0e02773c97b1"
],
"doc" : {
"_id" : "54b4d249f6a48ce54f8b6b9f",
"date" : NumberLong(1421136457),
"sender" : "52f37fbaf9cd0e02773c97b1",
"recipient" : "52ea178ff9cd0e9f24d776b4",
"isRead" : false,
"text" : "Hello 4!"
}
}