Mongodb-单据按状态查询分组
我有猫鼬模型-用户和门票 用户: 评论:Mongodb-单据按状态查询分组,mongodb,mongoose,mongodb-query,aggregation-framework,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我有猫鼬模型-用户和门票 用户: 评论: //Create schema const commentSchema = new Schema({ title: String, ticket_id: String, user: { type: Schema.Types.ObjectId, ref: 'User' }, status: { type: String, enum: ['New', 'O
//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']
},
...
}
我必须获得用户列表以及基于状态的评论数:
例如:
{
'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
},
我认为您需要在管道中使用$lookup:
$lookup: {
from: 'tickettest',
let: { userId: '$_id' },
pipeline: [{
$match: {
$expr: {
$eq: ['$assignedTo', '$$userId']
}
},
{
$project: {
status: 1,
}
},
{
$group: {
_id: '$status',
count: {
$sum: 1
}
}
},
}],
as: 'status_data'
}
我认为您需要在管道中使用$lookup:
$lookup: {
from: 'tickettest',
let: { userId: '$_id' },
pipeline: [{
$match: {
$expr: {
$eq: ['$assignedTo', '$$userId']
}
},
{
$project: {
status: 1,
}
},
{
$group: {
_id: '$status',
count: {
$sum: 1
}
}
},
}],
as: 'status_data'
}
您可以使用以下两级组聚合 样本文件:
db={
"users": [
{
"user_id": "user1",
"f_name": "user1 firstname",
"l_name": "user1 lastname"
},
{
"user_id": "user2",
"f_name": "user2 firstname",
"l_name": "user2 lastname"
}
],
"tickets": [
{
"title": "comment1",
"user": "user1",
"status": "New"
},
{
"title": "comment2",
"user": "user1",
"status": "New"
},
{
"title": "comment3",
"user": "user2",
"status": "New"
},
{
"title": "comment4",
"user": "user1",
"status": "Open"
},
{
"title": "comment5",
"user": "user2",
"status": "Open"
},
{
"title": "comment6",
"user": "user2",
"status": "Pending"
},
{
"title": "comment7",
"user": "user2",
"status": "Pending"
}
]
}
输出:
[
{
"f_name": "user2 firstname",
"l_name": "user2 lastname",
"status_data": [
{
"count": 2,
"status": "Pending"
},
{
"count": 1,
"status": "Open"
},
{
"count": 1,
"status": "New"
}
],
"userId": "user2"
},
{
"f_name": "user1 firstname",
"l_name": "user1 lastname",
"status_data": [
{
"count": 2,
"status": "New"
},
{
"count": 1,
"status": "Open"
}
],
"userId": "user1"
}
]
您可以使用以下两级组聚合 样本文件:
db={
"users": [
{
"user_id": "user1",
"f_name": "user1 firstname",
"l_name": "user1 lastname"
},
{
"user_id": "user2",
"f_name": "user2 firstname",
"l_name": "user2 lastname"
}
],
"tickets": [
{
"title": "comment1",
"user": "user1",
"status": "New"
},
{
"title": "comment2",
"user": "user1",
"status": "New"
},
{
"title": "comment3",
"user": "user2",
"status": "New"
},
{
"title": "comment4",
"user": "user1",
"status": "Open"
},
{
"title": "comment5",
"user": "user2",
"status": "Open"
},
{
"title": "comment6",
"user": "user2",
"status": "Pending"
},
{
"title": "comment7",
"user": "user2",
"status": "Pending"
}
]
}
输出:
[
{
"f_name": "user2 firstname",
"l_name": "user2 lastname",
"status_data": [
{
"count": 2,
"status": "Pending"
},
{
"count": 1,
"status": "Open"
},
{
"count": 1,
"status": "New"
}
],
"userId": "user2"
},
{
"f_name": "user1 firstname",
"l_name": "user1 lastname",
"status_data": [
{
"count": 2,
"status": "New"
},
{
"count": 1,
"status": "Open"
}
],
"userId": "user1"
}
]