我想每月获取数据。在我的表中数据存储如下: -
"patient" : [
{
"status" : 'arrived',
start_time: '2017-08-17T09:17:00.000Z
},
{
"status" : 'arraived',
start_time: '2017-08-16T07:17:00.000Z
},
{
"status" : 'arrived',
start_time: '2017-07-12T09:17:00.000Z
},
{
"status" : 'arraived',
start_time: '2017-07-05T08:10:00.000Z
},
{
"status" : 'arrived',
start_time: '2017-06-02T09:17:00.000Z
},
{
"status" : 'arraived',
start_time: '2017-05-05T08:16:00.000Z
}
]
等 我想把每个月的病人(jan到des)的总和,像这样: -
{
"month" : 8,
"count" : 2
}and like this month 1 to 12
答案 0 :(得分:1)
我假设,patient
数组与客户关联,日期以mongo ISO格式存储。
所以,实际文件看起来像是:
{
name: "stackOverflow",
"patient" : [
{
"status" : 'arrived',
"start_time": ISODate("2017-08-17T09:17:00.000Z")
},
{
"status" : 'arraived',
"start_time": ISODate("2017-08-16T07:17:00.000Z")
},
{
"status" : 'arrived',
"start_time": ISODate("2017-07-12T09:17:00.000Z")
},
{
"status" : 'arraived',
"start_time": ISODate("2017-07-05T08:10:00.000Z")
},
{
"status" : 'arrived',
"start_time": ISODate("2017-06-02T09:17:00.000Z")
},
{
"status" : 'arraived',
"start_time": ISODate("2017-05-05T08:16:00.000Z")
}
]
}
这是一个示例查询,您可以尝试 -
db.test.aggregate([
{$unwind: "$patient"},
{ $group: {
_id: {name: "$name", month: {$month: "$patient.start_time"}},
count: { $sum: 1}
}},
{$group: {
_id: "$_id.name",
patient: {$push: {month: "$_id.month", count: "$count"}}
}}
])
示例输出:
{
"_id" : "stackOverflow",
"patient" : [
{
"month" : 5,
"count" : 1
},
{
"month" : 6,
"count" : 1
},
{
"month" : 7,
"count" : 2
},
{
"month" : 8,
"count" : 2
}
]
}
您可以根据用例更改查询。希望这对你有帮助!
答案 1 :(得分:0)
这是我的代码: -
db.appointments.aggregate( [
{
$project:
{
"patient_id": 1,
"start_time": 1,
"status": 1
}
},
{
$match: {
'start_time' : { $gte: startdate.toISOString() },
'status': { $eq: 'arrived' }
} ,
},
{ $group: {
_id: {id: "$_id", start_time: {$month: "$appointments.start_time"}},
count: { $sum: 1}
}}
])
当我使用它时: -
{ $group: {
_id: {id: "$_id", start_time: {$month: "$start_time"}},
count: { $sum: 1}
}
}
显示错误消息: - {"名称":" MongoError","消息":"无法从缺少的BSON类型转换为日期",&# 34; ok":0," errmsg":"无法从BSON类型丢失转换为Date"," code":16006,& #34;代号":" Location16006"}
当我发表评论时,它显示了这一点: -
Out Put here: -
:[{"count":{"_id":"595b6f95ab43ec1f6c92b898","patient_id":"595649904dbe9525c0e036ef","start_time":"2017-07-04T10:35:00.000Z","status":"arrived"}},
{"计数" {" _id":" 595dff870960d425d4f14633"" patient_id":" 5956478b4dbe9525c0e036ea&#34 ;, " START_TIME":" 2017-03-08T09:14:00.000Z""状态":"抵"}},{&# 34;计数" {" _id":" 595dffaa0960d425d4f14634"" patient_id":" 595649904dbe9525c0e036ef"" START_TIME&# 34;:" 2017-03-17T09:15:00.000Z""状态":"抵"}},{"计数" :{" _id":" 595dffcf0960d425d4f14635"" patient_id":" 595648394dbe9525c0e036ec"" START_TIME":&#34 ; 2017-06-08T09:15:00.000Z""状态":"抵"}},{"计数" {" _id":" 595dfffb0960d425d4f14636"" patient_id":" 5956478b4dbe9525c0e036ea"" START_TIME":" 2017-06-20T09 :16:00.000Z""状态":"抵"}},{"计数" {" _id":& #34; 595e00160960d425d4f14637"" patient_id":" 5959ea7f80388b19e0b57817""开始_time":" 2017-08-17T09:17:00.000Z""状态":"抵"}}]}
答案 2 :(得分:0)
const group = {
$group: {
_id: { month: { $month: "$createdAt" } },
count: { $sum: 1 },
},
};
const groups = {
$group: {
_id: null,
patient: { $push: { month: '$_id.month', count: '$count' } },
},
};
return db.Patient.aggregate([group, groups]);