node.js - 如何计算猫鼬中开始日期和结束日期的总时间?
问题描述
我们的用户模式是这样的......
const docSchema = new mongoose.Schema({
userType: {
type: String,
enum: [
'customer',
'driver'
]
},
userId: {
type: String,
refPath: 'userType'
},
device: {
type: String,
enum: [
'Android',
'IOS'
]
},
appVersion: {
type: String,
default: ''
},
deviceId: {
type: String,
default: ''
},
connected: {
type: Date,
default: ''
},
disconnected: {
type: Date,
default: ''
}
}, {
timestamps: true
});
现在,我们必须计算用户使用连接和断开连接的总在线时间,任何人都可以帮助我们如何实现这一点?
解决方案
从您的问题来看,似乎对于每个会话(即一个连接和断开连接),您正在为DocSchema
[让,调用模型DocSchema
]中的用户创建一个文档。你想知道一个用户的总时间。您可以使用aggregate
.
这是一个aggregation
查找 userId 总时间的示例searchKey
Schema2.aggregate({
$match: {
userId: searchKey,
$project: {
diff: {
$subtract: ['$disconnected', '$connected']
}
},
$group: {
_id: 'justAGroupName',
total: {
$sum: '$diff'
}
}
}
});
有关更多信息,请查看mongodb 聚合文档
推荐阅读
- sqlite - 为什么 SQLITE 会创建空的 sqlite_sequence 表?
- c - 使用我自己的结构在套接字编程中填充 UDP 有效负载?
- xpath - 我只想从具有类名(我的类)的父级(div)中选择我选择的第 11 个孩子,其余的我忽略它们
- c++builder - 使用 TIdHTTP->Put() 编码 utf-8
- api - 如何在 ionic 4 的应用程序浏览器中为 instagram api 获取带有 loadstart 事件的 URL?
- linux - 在 Dockerfile 中替代 git clone
- netlogo - NetLogo:如何在“to setup ~ go”程序中的坐标(0 0)~(max-pxcor 0)中放置指定数量的海龟?
- javascript - 如果返回函数在别处声明,则柯里化不起作用
- python - 检查pandas df中列表中任何列名对之间的差异是否大于3
- c - 使用tensorflow的c api时如何禁用写入输出的tensorflow中的警告