Mongodb 基于两个文档之间的同一字段计算持续时间

Mongodb 基于两个文档之间的同一字段计算持续时间,mongodb,aggregation-framework,grouping,Mongodb,Aggregation Framework,Grouping,我有很多收藏品。我特别收藏了两种类型:1。与e-learning平台中内容可视化开始对应的集合;2.与此内容的可视化结束相对应的集合。以下是开始和结束可视化集合的示例: 开始可视化: "statement" : { "actor" : { "objectType" : "Agent", "name" : "exemple1@gmail.com", "mbox" : "mailto:exemple1@gmail.com" },

我有很多收藏品。我特别收藏了两种类型:1。与e-learning平台中内容可视化开始对应的集合;2.与此内容的可视化结束相对应的集合。以下是开始和结束可视化集合的示例:

开始可视化:

    "statement" : {
    "actor" : {
        "objectType" : "Agent",
        "name" : "exemple1@gmail.com",
        "mbox" : "mailto:exemple1@gmail.com"
    },
    "verb" : {
        "id" : "http://adlnet.gov/expapi/verbs/launched",
        "display" : {
            "en" : "Launching the next piece of learning content",
        }
    },
    "object" : {
        "objectType" : "Activity",
        "id" : "https://exemple1.com/activities/plannode=5857",
        "definition" : {
            "name" : {
                "en" : "calculs",
            },
            "type" : "http://id.tincanapi.com/activitytype/legacy-learning-standar"
        }
    },
    "context" : {
        "platform" : "exemple",
        "language" : "en",
    },
    "timestamp" : "2017-01-16T23:45:06+01:00",
    "id" : "0d7c852d-f1bf-45be-8a25-aff286bf1c30",
    "version" : "1.0.0"
},
        "statement" : {
    "actor" : {
        "objectType" : "Agent",
        "name" : "exemple1@gmail.com",
        "mbox" : "mailto:exemple1@gmail.com"
    },
    "verb" : {
        "id" : "http://adlnet.gov/expapi/verbs/terminated",
        "display" : {
            "en" : "Launching the next piece of learning content",
        }
    },
    "object" : {
        "objectType" : "Activity",
        "id" : "https://exemple1.com/activities/plannode=5857",
        "definition" : {
            "name" : {
                "en" : "calculs",
            },
            "type" : "http://id.tincanapi.com/activitytype/legacy-learning-standar"
        }
    },
    "context" : {
        "platform" : "exemple",
        "language" : "en",
    },
    "timestamp" : "2017-01-16T23:45:06+01:00",
    "id" : "0d7c852d-f1bf-45be-8a25-aff286bf1c30",
    "version" : "1.0.0"
},
末端可视化:

    "statement" : {
    "actor" : {
        "objectType" : "Agent",
        "name" : "exemple1@gmail.com",
        "mbox" : "mailto:exemple1@gmail.com"
    },
    "verb" : {
        "id" : "http://adlnet.gov/expapi/verbs/launched",
        "display" : {
            "en" : "Launching the next piece of learning content",
        }
    },
    "object" : {
        "objectType" : "Activity",
        "id" : "https://exemple1.com/activities/plannode=5857",
        "definition" : {
            "name" : {
                "en" : "calculs",
            },
            "type" : "http://id.tincanapi.com/activitytype/legacy-learning-standar"
        }
    },
    "context" : {
        "platform" : "exemple",
        "language" : "en",
    },
    "timestamp" : "2017-01-16T23:45:06+01:00",
    "id" : "0d7c852d-f1bf-45be-8a25-aff286bf1c30",
    "version" : "1.0.0"
},
        "statement" : {
    "actor" : {
        "objectType" : "Agent",
        "name" : "exemple1@gmail.com",
        "mbox" : "mailto:exemple1@gmail.com"
    },
    "verb" : {
        "id" : "http://adlnet.gov/expapi/verbs/terminated",
        "display" : {
            "en" : "Launching the next piece of learning content",
        }
    },
    "object" : {
        "objectType" : "Activity",
        "id" : "https://exemple1.com/activities/plannode=5857",
        "definition" : {
            "name" : {
                "en" : "calculs",
            },
            "type" : "http://id.tincanapi.com/activitytype/legacy-learning-standar"
        }
    },
    "context" : {
        "platform" : "exemple",
        "language" : "en",
    },
    "timestamp" : "2017-01-16T23:45:06+01:00",
    "id" : "0d7c852d-f1bf-45be-8a25-aff286bf1c30",
    "version" : "1.0.0"
},
现在,我想做的是按
object.id
对这些集合进行分组,并计算内容可视化的持续时间=时间戳(内容结束可视化)-时间戳(内容开始可视化)

我尝试了以下代码,但不起作用:

db.statements.aggregate([
{$match:{'statement.verb.id':{"$in":["http://adlnet.gov/expapi/verbs/launched", "http://adlnet.gov/expapi/verbs/terminated"]}}]},
{$group:{_id:{
        objectid: "$statement.object.id",
        learner: "$statement.actor.mbox"
        },
        duration : {$subtract: ["$timestamp", "$timestamp"}]}}
       }
     }
])


有谁能帮我计算这两个集合之间的持续时间(持续时间需要根据两个同名字段计算:timestamp.

您可以使用下面的聚合

db.statements.aggregate([
  {"$match":{"statement.verb.id":{"$in":["http://adlnet.gov/expapi/verbs/launched","http://adlnet.gov/expapi/verbs/terminated"]}}},
  {"$group":{
    "_id":{"objectid":"$statement.object.id","learner":"$statement.actor.mbox"},
    "first":{"$first":"$timestamp"},"second":{"$last":"$timestamp"}
  }},
  {"$addFields":{"duration":{"$abs":{"$subtract":["$first","$second"]}}}}
])