Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 查找用户在mongoDB中花费的总时间_Node.js_Mongodb_Mongoose_Mongodb Query_Aggregation Framework - Fatal编程技术网

Node.js 查找用户在mongoDB中花费的总时间

Node.js 查找用户在mongoDB中花费的总时间,node.js,mongodb,mongoose,mongodb-query,aggregation-framework,Node.js,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我有一个mongodb,其结构如下: |User |Location |TimeOfVisit |U1. |Cafeteria.|ISODate("2018-12-27T09:09:08.688Z") |U2. |Reception.|ISODate("2018-12-27T09:12:45.688Z") |U1. |Cafeteria.|ISODate("2018-12-27T09:45:38.688Z") |U1. |Cafeteria.|ISODate("2018-12-27T0

我有一个mongodb,其结构如下:

|User |Location  |TimeOfVisit
|U1.  |Cafeteria.|ISODate("2018-12-27T09:09:08.688Z")
|U2.  |Reception.|ISODate("2018-12-27T09:12:45.688Z")
|U1.  |Cafeteria.|ISODate("2018-12-27T09:45:38.688Z")
|U1.  |Cafeteria.|ISODate("2018-12-27T09:47:38.688Z")
我需要找到用户在任何特定位置花费的总时间。我读过多个博客,还没有找到任何具体的答案。我有以下资料:

aggregate([
        {
            "$match": {
                "User": {
                    "$eq": req.query.User
                }
            }
        },
        {
            "$group": {
                "_id": "$location",
                "totalMiniutes": {
                    <<Get the time>>
                }
            }
        }
聚合([
{
“$match”:{
“用户”:{
“$eq”:req.query.User
}
}
},
{
“$group”:{
“_id”:“$location”,
“totalMiniutes”:{
}
}
}

您可以从
TimeOfVisit
字段中找到
timeOfDay
,然后使用以获取总计数

db.collection.aggregate([
  { "$match": { "User": req.query.User }},
  { "$addFields": {
    "timeOfDay": {
      "$mod": [
        { "$toLong": "$TimeOfVisit" },
        1000*60*60*24
      ]
    }
  }},
  { "$group": {
    "_id": "$location",
    "totalTimeInMilliseconds": { "$sum": "$timeOfDay" }
  }}
])

您可以进一步将其划分为天、小时、分钟或秒

1小时=60分钟=60×60秒=3600秒=3600×1000毫秒=3600000毫秒。

db.collection.aggregate([
  { "$addFields": {
    "timeOfDay": {
      "$mod": [
        { "$subtract": [ "$TimeOfVisit", Date(0) ] },
        1000 * 60 * 60 * 24
      ]
    }
  }},
  { "$group": {
    "_id": "$location",
    "totalTimeInMiniutes": {
      "$sum": { "$divide": ["$timeOfDay", 60 × 1000] }
    }
  }}
])
对于mongodb4.0及以上版本

db.collection.aggregate([
  { "$addFields": {
    "timeOfDay": {
      "$mod": [
        { "$toLong": "$TimeOfVisit" },
        1000 * 60 * 60 * 24
      ]
    }
  }},
  { "$group": {
    "_id": "$location",
    "totalTimeInMiniutes": {
      "$sum": { "$divide": ["$timeOfDay", 60 × 1000] }
    }
  }}
])

您如何计算这样的持续时间?您不存储到达和离开的时间戳。出发时间戳可以假定为表中的下一个时间戳。即| U2。|接收。| ISODate(“2018-12-27T09:12:45.688Z”)可以是出发时间戳,但这给出了每个时间戳的分钟数,而不是在每个区域花费的总时间,对吗?确定更新了答案。它将给出以毫秒为单位的总时间更新答案。现在,您将获得以分钟为单位的总时间“无法识别的表达式“$toLong”mongo 3.x版有其他版本吗?
db.collection.aggregate([
  { "$addFields": {
    "timeOfDay": {
      "$mod": [
        { "$toLong": "$TimeOfVisit" },
        1000 * 60 * 60 * 24
      ]
    }
  }},
  { "$group": {
    "_id": "$location",
    "totalTimeInMiniutes": {
      "$sum": { "$divide": ["$timeOfDay", 60 × 1000] }
    }
  }}
])