数据:
{
"_id" : ObjectId("5874a303414af4d70e3c988d"),
"trade" : "XYZ",
"sub_trade" : [
"ABC1",
"ABC2",
"ABC3",
"ABC14",
"ABC15"
]
}
我们应该将sub_trade的索引动态地改为交易值
接受输出:
{
"_id" : ObjectId("5874a303414af4d70e3c988d"),
"trade" : "XYZ",
"XYZ" : [
"ABC1",
"ABC2",
"ABC3",
"ABC14",
"ABC15"
]
}
请帮帮我
答案 0 :(得分:0)
你不想要这样的东西:
data['sub_trade'] = data['trade'];
delete data['trade'];
编辑:如果要在查询时更改使用Mongos投影
{
"$project": {
"sub_trade": "trade"
}
}
https://docs.mongodb.com/manual/reference/operator/aggregation/project/