This seems like it should be easy but it seems to be annoyingly hard.
Input collection contains documents like this:
{
"foo": [
{
"userId": "e736f24f-164e-46d1-8a8b-6ac2f69237a8",
"params": {
"location": {
"coordinates": {
"first": -80.514124,
"second": 45.509854
}
},
"withinDistance": 30,
... many other fields ...
},
},
{
"userId": "b71d2067-9dee-45cc-9903-7dea371ea685",
"params": {
"location": {
"coordinates": {
"first": -82.923723,
"second": 47.210577
}
},
"withinDistance": 30,
... many other fields ...
},
},
]
}
And I want to transform the non-standard foo.params.location.coordinates
object into the standard GeoJSON format, so the output should look like this:
{
"foo": [
{
"userId": "e736f24f-164e-46d1-8a8b-6ac2f69237a8",
"params": {
"location": {
"type": "Point",
"coordinates": [
-80.514124,
45.509854
]
},
"withinDistance": 30,
... many other fields ...
},
},
{
"userId": "b71d2067-9dee-45cc-9903-7dea371ea685",
"params": {
"location": {
"type": "Point",
"coordinates": [
-82.923723,
47.210577
]
},
"withinDistance": 30,
... many other fields ...
},
},
]
}
The closest I’ve come is this but I lose the withinDistance
and other fields in the params
structure:
db.collection.updateMany({}, [
{
"$set": {
"foo": {
"$map": {
"input": "$foo",
"as": "f",
"in": {
$mergeObjects: [
"$$f",
{
"params": {
"location" : {
"type": "Point",
"coordinates": [
"$$f.params.location.coordinates.first",
"$$f.params.location.coordinates.second"
]
}
}
}
]
}
}
}
}
}
])