我有一个查询,该查询按年份分组返回每年每月集合中的条目总数。如果该位置具有所讨论的年份月份的结果,则此返回的数据完全符合我的需要。但是,是否可以插入一个没有结果的月份的条目?例如,假设我的$ match的日期范围是01-2019到12-2019。我希望该月所有12个条目都具有默认的总数:0。这是否可能
截断的架构:
{
branchId: { type: String, required: true },
orgId: { type: String, required: true },
stars: { type: Number, default: 0 },
reviewUpdatedAt: { type: Date, default: Date.now }
}
示例查询:
[
{
$match: {
stars: { $exists: true, $gte: 1 },
orgId: '100003'
reviewUpdatedAt: { $gte: new Date(fromDate), $lte: new Date(toDate) }
}
},
{
$group: {
_id: {
date: {
$dateToString: {
format: "%m-%Y",
date: "$reviewUpdatedAt"
}
},
loc: "$branchId"
},
total: {
$sum: 1
}
}
},
{
$group: {
_id: "$_id.loc",
reviews: {
$push: {
total: "$total",
"date": "$_id.date"
}
}
}
}
]
fromDate是June-2018,而
toDate是June-2019,那么通过使用编程语言代码,您可以轻松地以mm-yyyy]格式获取这两个日期之间的所有月份。 。您可以尝试使用MongoDB进行此操作,但我更希望将其作为查询的输入。
查询:db.collection.aggregate([
{
$group: {
_id: {
date: {
$dateToString: {
format: "%m-%Y",
date: "$reviewUpdatedAt"
}
},
loc: "$branchId"
},
Total: {
$sum: 1
}
}
},
{
$group: {
_id: "$_id.loc",
reviews: {
$push: {
Total: "$Total",
"date": "$_id.date"
}
}
}
},
/** Overwrite existing reviews field with new array, So forming new array ::
* as you're passing all months between these dates get a difference of two arrays (input dates - existing dates after group)
* while will leave us with an array of missing dates, we would iterate on that missing dates array &
* concat actual reviews array with each missing date
* */
{
$addFields: {
reviews: {
$reduce: {
input: {
$setDifference: [
[
"06-2018",
"07-2018",
"08-2018",
"09-2018",
"10-2018",
"11-2018",
"12-2018",
"01-2019",
"02-2019",
"03-2019",
"04-2019",
"05-2019",
"06-2019"
],
"$reviews.date"
]
},
initialValue: "$reviews",
in: {
$concatArrays: [
"$$value",
[
{
date: "$$this",
Total: 0
}
]
]
}
}
}
}
}
])
测试:
MongoDB-Playground