为什么我不能在我的开关盒中使用我的投影场?

Why cant i use my projection field in my switch case?

我正在尝试通过针对月份的案例将名为 'summary' 的时区字段应用于我的文档,为什么我不能在我的切换案例中使用 'month' 投影字段?

 db.steps.aggregate( [
      {
        $project:
          {
            patientName : 1,
            month: {$month: '$dateTime'},
            hour: {$hour: '$dateTime'},
            minute: {$minute: '$dateTime'},
            day: {$dayOfMonth: '$dateTime'},
            "summary" :
            {
              $switch:
                {
                  branches: [
                    {
                      case: { $eq : [ '$month', 6 ] },
                      then: "month 6"
                    },
                    {
                      case: { $eq : [ '$month', 7 ] },
                      then: "month 7"
                    }
                  ],
                  default: "No value found."
                }
             }
          }
       },
       { $group: { _id: {"day":"$day", "month":"$month", "summary":"$summary"}}}
    ]);

它没有得到正确的然后它只是进入默认值:

{ "_id" : { "day" : 24, "month" : 9, "summary" : "No value found." } }
{ "_id" : { "day" : 29, "month" : 9, "summary" : "No value found." } }
{ "_id" : { "day" : 22, "month" : 6, "summary" : "No value found." } }
{ "_id" : { "day" : 19, "month" : 7, "summary" : "No value found." } }
{ "_id" : { "day" : 6, "month" : 8, "summary" : "No value found." } }
{ "_id" : { "day" : 2, "month" : 9, "summary" : "No value found." } }
{ "_id" : { "day" : 24, "month" : 7, "summary" : "No value found." } }
{ "_id" : { "day" : 6, "month" : 9, "summary" : "No value found." } }
{ "_id" : { "day" : 31, "month" : 7, "summary" : "No value found." } }
{ "_id" : { "day" : 10, "month" : 9, "summary" : "No value found." } }
{ "_id" : { "day" : 12, "month" : 9, "summary" : "No value found." } }
{ "_id" : { "day" : 22, "month" : 9, "summary" : "No value found." } }
{ "_id" : { "day" : 13, "month" : 9, "summary" : "No value found." } }

同台不能用投影场吗?

谢谢

您无权访问同一阶段的字段。这里有两种可能的解决方案:

db.steps.aggregate( [
      {
        $project:
          {
            patientName : 1,
            month: {$month: '$dateTime'},
            hour: {$hour: '$dateTime'},
            minute: {$minute: '$dateTime'},
            day: {$dayOfMonth: '$dateTime'},
            "summary" :
            {
              $switch:
                {
                  branches: [
                    {
                      case: { $eq : [ {$month: '$dateTime'}, 6 ] },
                      then: "month 6"
                    },
                    {
                      case: { $eq : [ {$month: '$dateTime'}, 7 ] },
                      then: "month 7"
                    }
                  ],
                  default: "No value found."
                }
             }
          }
       },
       { $group: { _id: {"day":"$day", "month":"$month", "summary":"$summary"}}}
    ]);



db.steps.aggregate( [
      {
        $project:
          {
            patientName : 1,
            month: {$month: '$dateTime'},
            hour: {$hour: '$dateTime'},
            minute: {$minute: '$dateTime'},
            day: {$dayOfMonth: '$dateTime'}
          }
       },
      {
        $set: // $project would remove all others
          {
            "summary" :
            {
              $switch:
                {
                  branches: [
                    {
                      case: { $eq : [ '$month', 6 ] },
                      then: "month 6"
                    },
                    {
                      case: { $eq : [ '$month', 7 ] },
                      then: "month 7"
                    }
                  ],
                  default: "No value found."
                }
             }
          }
       },
       { $group: { _id: {"day":"$day", "month":"$month", "summary":"$summary"}}}
    ]);

注意,我更喜欢

{ $cond: 
   { 
      if: {$in: ['$month', [6,7]]}, 
      then: { $concat: [ "month ", '$month' ] }, 
      else: "No value found."
   } 
}