My documents contain a field called "details" which is an array:
{
"_id" : ObjectId("60ae0b29ab282518443c7ac5"),
"details": [
{
"label": "Asset title",
"value": "S1",
},
{
"label": "Total Cost",
"value": "250",
},
{
"label": "Possession Status",
"value": "Available",
},
{
"label": "Estimated Monthly Rent",
"value": "15.5",
}
]
},
{
"_id" : ObjectId("60ae0b29ab282518443c7ac8"),
"details": [
{
"label": "Asset title",
"value": "S2",
},
{
"label": "Total Cost",
"value": "455.5",
},
{
"label": "Possession Status",
"value": "Available",
},
{
"label": "Estimated Monthly Rent",
"value": "30",
}
]
}
I am currently attempting to $project
this array of objects by mapping through each object and converting certain string values to numbers when the label is either "Total Cost" or "Estimated Monthly Rent". For example, for {label = "Total Cost"}
, I want to use: {"$toDouble" : value}
, and for
{label = "Estimated Monthly Rent"}
, I want to apply: {"$toDouble" : value}
.
However, my attempt to do so is resulting in an error.
db.collection.aggregate([
{
"$project": {
"data": {
"$map": {
"input": "$details",
"as": "val",
"in": {
"$cond":{ if: {"$$val.label": "Total Cost"} , then: { "$toDouble" : "$$val.value"}},
"$cond":{ if: {"$$val.label": "Estimated Monthly Rent"} , then: { "$toDouble" : "$$val.value"}}
}
}
}
}
}
])