Aggregation Mongodb - 计算两个时间戳之间的时差

时间:2017-06-22 15:19:07

标签: mongodb aggregation-framework

集合中的数据:

public static void main(String[] args) {
    String json = "{\"code\":100,\n" +
            "\n" +
            " \"name\":\"3PVxkvfKyUiBg3LN24ek23KceGg6350KSkLZ.html\",\n" +    
            "\n" +
            "  \"file\":{\n" +
            "         \"author\":\"test@test\",\n" +
            "\n" +
            "         \"idx\":\"xihav-zupak-zonyf-bedid-cyvun-fahac-mykud\",\n" +
            "\n" +
            "         \"name\":\"html_file\",\n" +
            "\n" +
            "         \"references\":[\n" +
            "                \"relec-toluz\",\n" +
            "\n" +
            "                \"rosah-vyzyh\",\n" +
            "\n" +
            "                \"rikik-cinom\"\n" +
            "        ]\n" +
            "   }\n" +
            "}";
    JsonElement element = new JsonParser().parse(json);
    System.out.println(Arrays.toString(getReferences(element)));

}

private static String[] getReferences(JsonElement jsonElement) {
    List<String> refList = new ArrayList<>();
    JsonArray references = jsonElement.getAsJsonObject().get("file").getAsJsonObject().get("references").getAsJsonArray();
    references.forEach((reference) -> refList.add(reference.getAsString()));
    return refList.toArray(new String[0]);
}

我的目标是:

{
    user:1
    created: 1487593990260
    header: {
        time: 1487593987000
    }
}
{
    user:2
    created: 1487594980677
    header: {
        time: 1487594978000
    }
}
{
    user:3
    created: 1487595199046
    header: {
        time: 1487595197000
    }
}
{
    user:2
    created: 1487595452715
    header: {
        time: 1487595450000
    }
}
{
    user:3
    created: 1487622649917
    header: {
        time: 1487622649000
    }
}
{
    user:1
    created: 1487622654169
    header: {
        time: 1487622654000
    }
}
{
    user:2
    created: 1487623129875
    header: {
        time: 1487623127000
    }
}
{
    user:2
    created: 1487623298718
    header: {
        time: 1487623297000
    }
}
{
    user:1
    created: 1487623336638
    header: {
        time: 1487623335000
    }
}

然后我想计算创建之间的时间 - &gt;时间 - &gt;创建等......

我要做的最后一件事是按用户分组以查看时差。所以我可以计算每个用户的平均时间。

但我不知道聚合框架是否可行? 或者我是否必须在客户端中执行此操作,因为它是用于显示聊天的图表?

0 个答案:

没有答案