mongodb聚合统计

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 

你可能感兴趣的:(mongodb,java)