Čam*_*amo 5 field key-value mongodb
有人可以告诉我在mongodb结果中是否可以使用字段值作为键。如果我有类似的文件
{'code': 'xxx', 'item': 'yyy'}
{'code': 'ooo', 'item': 'eee'}
Run Code Online (Sandbox Code Playgroud)
我想获得结果,其中代码值将是关键
{'xxx': 'yyy'}, {'ooo': 'eee'}
Run Code Online (Sandbox Code Playgroud)
    如果要动态构建密钥,则必须使用$ arrayToObject。它以k和v字段组成的数组作为参数。要使其成为根,可以使用$ replaceRoot阶段,请尝试:
db.col.aggregate([
    {
        $replaceRoot: {
            newRoot: { $arrayToObject: [ [ { k: "$code", v: "$item" } ] ]}
        }
    }
])
Run Code Online (Sandbox Code Playgroud)