【发布时间】:2018-06-20 23:48:29
【问题描述】:
我有一个包含这种形式的文档的集合:
{
"name" : "John Smith",
"store_affiliation" : {
"stores" : {
"ABCD" : {
"role" : "General Manager",
"startdate" : ISODate("1970-01-01T00:00:00.000+0000"),
"enddate" : ISODate("1980-01-01T00:00:00.000+0000"),
"permissions" : "GM"
},
"1234" : {
"role" : "Owner",
"startdate" : ISODate("1970-01-01T00:00:00.000+0000"),
"enddate" : null,
"permissions" : "ALL"
},
"4321" : {
"role" : "Owner",
"startdate" : ISODate("1990-01-01T00:00:00.000+0000"),
"enddate" : null,
"permissions" : "ALL"
}
}
}
...但我需要商店列表采用这种形式(“商店”数组):
{ "name" : "John Smith",
"store_affiliation" : {
"stores" : [
{
"store_code" : "ABCD",
"role" : "General Manager",
"startdate" : ISODate("1970-01-01T00:00:00.000+0000"),
"enddate" : ISODate("1980-01-01T00:00:00.000+0000"),
"permissions" : "GM"
},
{
"store_code" : "1234",
"role" : "Owner",
"startdate" : ISODate("1970-01-01T00:00:00.000+0000"),
"enddate" : null,
"permissions" : "ALL"
},
{
"shop_id" : "4321",
"role" : "Owner",
"startdate" : ISODate("1990-01-01T00:00:00.000+0000"),
"enddate" : null,
"permissions" : "ALL"
}
]
}
我研究过在aggregate 管道中使用$project、$group 和$push,但我觉得使用aggregate 甚至可能是死路一条,因为我不是在查询结果;我正在尝试永久修改集合中的每个文档(数千个)。
【问题讨论】:
标签: mongodb aggregation-framework