Mongodb 获取聚合框架中的最后一个组合 [英] Mongodb get last combination in aggregation framework

查看:14
本文介绍了Mongodb 获取聚合框架中的最后一个组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一组消息:

{
  "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.

例如,对于 ID = '52f37fbaf9cd0e02773c97b1' 的用户,应该获得 3 条记录.

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" }
    }}
])

这将您的样本范围缩小到发件人/收件人"的唯一"3 个组合,并返回该对"之间对话中的最后一个文档:

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!"
    }
}

这篇关于Mongodb 获取聚合框架中的最后一个组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆