我们的用户架构如下...
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
});
现在,我们必须使用连接和断开连接计算用户的总在线时间, 任何人都可以帮助我们,我们如何实现这一目标?
答案 0 :(得分:1)
从您的问题看来,对于每个会话(即一个连接和断开连接),您正在DocSchema
中为用户创建文档[让我们将模型称为{{ 1}}]。您想知道用户的总时间。您可以使用DocSchema
。
这是一个示例aggregate
,用于查找userId aggregation
的总时间
searchKey
有关更多信息,请检查mongodb aggregation doc