我的文档结构如下:
{
"name":"CategoryChildLevel2",
"parentId":"2",
"otherAttribute":"anyVal",
"breadcrumb":[
{
"name":"RootCategory",
"id":"1"
},
{
"name":"CategoryChildLevel1",
"id":"2"
},
{
"name":"CategoryChildLevel2",
"id":"3"
}
]
}
我想要的是能够运行一个查询:
替换以开头的面包屑数组
{
"name":"RootCategory",
"id":"1"
},
{
"name":"CategoryChildLevel1",
"id":"2"
}
将此子序列替换为
{
"name":"RootCategory",
"id":"1"
},
{
"name":"AnotherCategory",
"id":"4"
}
{
"name":"AnotherCategory2",
"id":"5"
}
所以最终的结果是
{
"name":"CategoryChildLevel2",
"parentId":"2",
"otherAttribute":"anyVal",
"breadcrumb":[
{
"name":"RootCategory",
"id":"1"
},
{
"name":"AnotherCategory",
"id":"4"
},
{
"name":"AnotherCategory2",
"id":"5"
},
{
"name":"CategoryChildLevel2",
"id":"3"
}
]
}
我们可以在 MongoDB 中做到这一点吗?或者至少array startsWith
使用普通查询语言或 map/reduce 检索我们应该更新(查询)的项目?