db.dept.aggregate([
{
$project: {
_id: "$id",
name: "$name"
}
},
{
$lookup: {
from: "story",
localField: "_id",
foreignField: "initialDeptId",
as: "stories"
}
},
{
$unwind: {
path: "$stories",
preserveNullAndEmptyArrays: true
}
},
{
$match: {
$or: [
{
"stories.statusName": { $nin: ["草稿", "未知状态", "删除"] },
"stories.submitDate": {
$gte: ISODate("2023-01-01"),
$lte: ISODate("2023-11-01")
}
},
{
"stories": { $exists: false }
}
]
}
},
{
$group: {
_id: "$_id",
departmentName: { $first: "$name" },
totalSubmissions: { $sum:
{ $cond: [{ $ifNull: ["$stories", false] }, 1, 0] } //$cond
文章来源地址https://www.toymoban.com/news/detail-741418.html
文章来源:https://www.toymoban.com/news/detail-741418.html
到了这里,关于mongodb聚合统计的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!