MongoDB 使用 reduce "$$this" 变量作为对象键

MongoDB use reduce "$$this" variable as an object key

当前代码:

let someObject = {a: "hello ", b: "world", c: "!"};
...
// QUERY
$reduce: {
   input: ["a", "b", "c"],
   initialValue: "",
   in: { $concat : ["$$value", someObject["$$this"]] }
}

预期输出:

hello world!

当前输出:

null

原因:“$$this”被认为是"$$this" string,而不是作为reduce变量$$this 用作方括号中的 属性 时。 someObject["$$this"]undefined.

修正: ???

我已经尝试了所有方法,在 mongoose.Types.ObjectId() 中使用它,在带有空字符串的嵌套 $concat 中使用它,还有很多我现在已经忘记的其他东西。我想我错过了什么。我怎样才能强制它成为 $$this 变量而不是字符串 "$$this"

MongoDB 聚合框架有 $arrayElemAt,但没有 $objectElemAt 可以传递 expression 来获取对象值。

解决方法: 我们需要将对象转换为数组,过滤 key:value 和 return 的值。

{a:"hello", b:"world", c:"!"} 
to 
[ {k:"a", v:"hello"}, {k:"b", v:"world"}, {k:"c", v:"!"} ]

试试这个:

db.collection.aggregate([
  {
    $addFields: {
      data: {
        $reduce: {
          input: {
            $map: {
              input: ["a","b","c"],
              as: "abc",
              in: {
                $let: {
                  vars: {
                    someObject: {
                      $filter: {
                        input: {
                          $objectToArray: {a: "hello ",b: "world",c: "!"}
                        },
                        cond: {
                          $eq: ["$$this.k","$$abc"]
                        }
                      }
                    }
                  },
                  in: {
                    $ifNull: [
                      {
                        $arrayElemAt: ["$$someObject.v", 0]
                      },
                      ""
                    ]
                  }
                }
              }
            }
          },
          initialValue: "",
          in: {
            $concat: ["$$value","$$this"]
          }
        }
      }
    }
  }
])

MongoPlayground