'How to Map and reduce an array of Strings to a single object with multiple values

I have a document that looks like such:

{
  _id: ObjectId("6222ca4252925ad4c3faec08"),
  value: ["test1","test2"]
}

I would like to get:

{
  _id: ObjectId("6222ca4252925ad4c3faec08"),
 “value”:
    {  
      “value1”: "test1",
      “value2”: "test2"
     }
}

I have tried to use reduce but I get each value in one object but am very close. Please let me know what I am doing wrong:

Code:

{
  "$project": {
    "value": {
      $reduce: {
        input: "$value",
        initialValue: [],
        in: {$concatArrays: [
          "$$value", 
          [{"name": "$$this"}]
        ]}
      }
    }
  }
}

Results:

{ 
    "_id" : ObjectId("6222ca4252925ad4c3faec08"), 
    "value" : [
        {
            "name" : "test1"
        }, 
        {
            "name" : "test2"
        }
    ]
}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source