【发布时间】:2019-12-13 23:03:01
【问题描述】:
我有猫鼬模型 - 用户和工单
用户:
//Create schema
const userSchema = new Schema({
title: String,
f_name: {
type: String,
required: true
},
m_name: String,
l_name: {
type: String,
required: true
},
...
}
评论:
//Create schema
const commentSchema = new Schema({
title: String,
ticket_id: String,
user: {
type: Schema.Types.ObjectId,
ref: 'User'
},
status: {
type: String,
enum: ['New', 'Open', 'Pending', 'Suspended', 'Solved']
},
...
}
我必须根据状态获取用户列表以及 cmets 计数: 示例:
{
'f_name': 'XYZ',
...,
status_data: {
[
{
'status': 'New',
'count': 3
},
{
'status': 'Solved',
'count': 3
},
{
'status': 'Suspended',
'count': 3
}
]
}
}
目前我正在尝试:
User.aggregate([
{
$lookup: {
from: 'tickettests',
localField: '_id',
foreignField: 'assigned_to',
as: 'tickets_doc'
}
},
{
$unwind: {
path: '$tickets_doc'
}
},
{
$group: {
_id: {
_id: '$_id',
status: '$tickets_doc.status'
},
user_id: {$first: '$_id'},
f_name: {$first: '$f_name'},
l_name: {$first: '$l_name'},
createdAt: {$first: '$createdAt'},
updatedAt: {$first: '$updatedAt'},
count: {
$sum: 1
}
}
}
])
.then(ticketPromisesRes => {
res.status(200).json(ticketPromisesRes);
})
.catch(err => {
logger.error(err);
res.status(500).json({message: 'Cannot get contact list with tickets'})
})
数据像往常一样聚合,有重复的用户,但它的状态和数量是唯一的,我试图让它像上面给出的响应一样,
目前的回应:
{
"_id": {
"_id": "xxxxx",
"status": "New"
},
"user_id": "xxxxx",
"f_name": "XYZ",
"l_name": "ZXC",
"createdAt": "2019-12-07T04:43:03.839Z",
"updatedAt": "2019-12-07T04:45:59.322Z",
"count": 2
},
{
"_id": {
"_id": "xxxxxx",
"status": "Suspended"
},
"user_id": "xxxxxx",
"f_name": "ASD",
"l_name": "DSA",
"createdAt": "2019-12-07T04:28:13.616Z",
"updatedAt": "2019-12-07T04:37:03.737Z",
"count": 1
},
{
"_id": {
"_id": "xxxxxxx",
"status": "Suspended"
},
"user_id": "xxxxxxx",
"f_name": "XYZ",
"l_name": "ZXC",
"createdAt": "2019-12-07T04:43:03.839Z",
"updatedAt": "2019-12-07T04:45:59.322Z",
"count": 1
},
【问题讨论】:
标签: mongodb mongoose mongodb-query aggregation-framework