Original data:
{ version: "1.002", subVersion: "3" }
How do I change it into an Object:
{ version: { version1: 1, version2: 2, version3: 3 } }
In the new object, the fields are integer and they come from the string fields of version and subVersion from original data.
Thanks!
Cast_Away
(Cast Away)
#2
The are lots of ways this could be done. One way, given:
Example document:
{
version: "1.002",
subVersion: "3"
}
Example aggregation pipeline using "$set"
:
db.collection.aggregate([
{
"$set": {
"version": {
"$let": {
"vars": {
"splitVer": {
"$map": {
"input": {"$split": ["$version", "."]},
"as": "strNum",
"in": {"$toInt": "$$strNum" }
}
}
},
"in": {
"version1": {"$first": "$$splitVer"},
"version2": {"$last": "$$splitVer"},
"version3": {"$toInt": "$subVersion"}
}
}
},
"subVersion": "$$REMOVE"
}
}
])
Example output:
{
"_id": ObjectId("5a934e000102030405000000"),
"version": {
"version1": 1,
"version2": 2,
"version3": 3
}
}
Try it on mongoplayground.net.
1 Like
This is great! Thanks so much!!
1 Like