I have a MongoDB document like this:
{
"_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"
}
]}
I would like to transform it into this:
{
"_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']
}
]}
This has to be done during the aggregation. I was trying to to do this like this:
{ "$project":{
"assets.nested_field_to_split":{
"$split":[
"$assets.nested_field_to_split",
"##"
]
}
}
}
I have found in the documentation (https://docs.mongodb.com/manual/reference/operator/aggregation/project/) that "When nesting the fields, you cannot use dot notation inside the embedded document to specify the field, e.g. contact: { "address.country": <1 or 0 or expression> } is invalid.". So my guess it can't be done. Not like I'm trying at least. I was trying to go around it with a $map or $filter operator but with no success obviously. Please help.