使用自定义键使用 MongoDB $arrayToObject

考虑以下作为聚合管道输出的文档:

{ "_id" : 1,"results" : [
  { "status" : "HOLD","footage" : 43.01,"pieces" : 1 },{ "status" : "accEPTED","footage" : 80.90,"pieces" : 2 },{ "status" : "REJECTED","footage" : 40.00,"pieces" : 1 }
]}

我正在尝试创建将状态与镜头和片段字段连接起来的自定义键,结果:

{ "_id" : 1,"results" :
  {
    "HOLD_footage": 43.01,"HOLD_pieces":1,"accEPTED_footage": 80.90,"accEPTED_pieces": 2,"REJECTED_footage":40.00,"REJECTED_pieces":1
  }
}
xzgfzcnk 回答:使用自定义键使用 MongoDB $arrayToObject

您可以执行以下操作:

  1. 使用 describe(['QA'],'Will tag every test inside the describe with the "QA" tag',function () { ... }); it(['QA'],'This is a work-in-progress test',function () { ... }); 使用您想要的密钥构建 k-v 元组(即 HOLD_footage...)
  2. 使用 $map 对 k-v 元组数组进行分组
  3. 使用 $concatArrays 取回您期望的数组形式

这是 Mongo playground 供您参考。

本文链接:https://www.f2er.com/203.html

大家都在问