【发布时间】:2020-11-14 03:49:39
【问题描述】:
我有一个这样的 MongoDB 文档:
{
"_id" : ObjectId("5c949609ff5e3d6119758730"),
"product_name" : "Shoes",
"field_to_split" : "one##two##three##four",
"assets" : [
{
"url" : "www.blabla.com/1",
"nested_field_to_split" : "one##two##three##four"
},
{
"url" : "www.blabla.com/2",
"nested_field_to_split" : "one##two##three##four"
},
{
"url" : "www.blabla.com/3",
"nested_field_to_split" : "one##two##three##four"
},
{
"url" : "www.blabla.com/4",
"nested_field_to_split" : "one##two##three##four"
}
]}
我想把它变成这样:
{
"_id" : ObjectId("5c949609ff5e3d6119758730"),
"product_name" : "Shoes",
"field_to_split" : "one##two##three##four",
"assets" : [
{
"url" : "www.blabla.com/1",
"nested_field_to_split" : ['one', 'two', 'three', 'four']
},
{
"url" : "www.blabla.com/2",
"nested_field_to_split" : ['one', 'two', 'three', 'four']
},
{
"url" : "www.blabla.com/3",
"nested_field_to_split" : ['one', 'two', 'three', 'four']
},
{
"url" : "www.blabla.com/4",
"nested_field_to_split" : ['one', 'two', 'three', 'four']
}
]}
这必须在聚合期间完成。我试图这样做:
{ "$project":{
"assets.nested_field_to_split":{
"$split":[
"$assets.nested_field_to_split",
"##"
]
}
}
}
我在文档 (https://docs.mongodb.com/manual/reference/operator/aggregation/project/) 中发现“嵌套字段时,不能在嵌入文档中使用点表示法来指定字段,例如 contact: { "address.country": } 无效。”。所以我的猜测是做不到的。至少不像我在尝试。我试图用 $map 或 $filter 运算符绕过它,但显然没有成功。请帮忙。
【问题讨论】:
标签: mongodb split aggregation projection