Mongo DB聚合自平均加入

时间:2016-03-24 15:22:55

标签: mongodb join mongodb-query aggregation-framework

我需要使用聚合框架制作足够复杂的技巧。

我正在聚合操作状态更改的日志。 (我猜很常见)

经过几个管道步骤,我得到了类似的东西:

    {
    "operaiton_id" : "56cf29e90c15c45630347320",
    "timestamp" : 1456417257087,
    "old_state" : "OPEN",
    "new_state" : "IN_EXAM"
    },

    { 
    "operaiton_id" : "56cf29e90c15c45630347320",
    "timestamp" : 1456481305971,
    "old_state" : "IN_EXAM",
    "new_state" : "IN_ALERT"
    },

    ...
    { 
    "operaiton_id" : "33ab23f92999c40638553320",
    "timestamp" : 1458147304895,
    "old_state" : "OPEN",
    "new_state" : "IN_EXAM"
    },

    { 
    "operaiton_id" : "33ab23f92999c40638553320",
    "timestamp" : 1458147310251,
    "old_state" : "IN_EXAM",
    "new_state" : "IN_ALERT"
    }

我在这里寻找的是从每个州到每个州的平均过渡时间。有一个SQL我自己加入,有点:

SELECT AVG(a.timestamp - b.timestamp)
FROM a JOIN a AS b ON a.old_state=b.new_state

任何想法我怎么能在Mongo Aggregation中做到这一点?感到绝望。

0 个答案:

没有答案