Created
October 17, 2017 17:26
-
-
Save terakilobyte/4742624acf19bd2e35a38df0705d4fdd to your computer and use it in GitHub Desktop.
l trim, r trim, and trim in MongoDB's Aggregation Framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.trim.insertMany([ | |
{ | |
"_id" : ObjectId("59e0d1e8505f76f4acde0609"), | |
"a" : " \t\n\r abc \t\n\r " | |
} | |
{ "_id" : ObjectId("59e4d34cd1fb8f891853bca4"), "a" : "xy" } | |
{ "_id" : ObjectId("59e4e55dd1fb8f891853bca5"), "a" : " " } | |
{ "_id" : ObjectId("59e4e56fd1fb8f891853bca6"), "a" : " " } | |
{ "_id" : ObjectId("59e4e578d1fb8f891853bca7"), "a" : " a " } | |
]) | |
db.trim.aggregate([ | |
{ | |
$project: { | |
_id: 0, | |
a: 1, | |
a_split: { | |
$let: { | |
vars: { | |
str_range: { | |
$range: [0, { $strLenCP: "$a" }] | |
} | |
}, | |
in: { | |
$let: { | |
vars: { | |
split_on_CP_str: { | |
$reduce: { | |
input: "$$str_range", | |
initialValue: { | |
split_string: [], | |
index: 0 | |
}, | |
in: { | |
split_string: { | |
$concatArrays: [ | |
"$$value.split_string", | |
[ | |
{ | |
$substrCP: ["$a", "$$value.index", 1] | |
} | |
] | |
] | |
}, | |
index: { $add: ["$$value.index", 1] } | |
} | |
} | |
} | |
}, | |
in: "$$split_on_CP_str.split_string" | |
} | |
} | |
} | |
}, | |
rtrimmed_a: { | |
$substrCP: [ | |
"$a", | |
0, | |
{ | |
$max: { | |
$let: { | |
vars: { | |
str_range: { | |
$range: [0, { $strLenCP: "$a" }] | |
} | |
}, | |
in: { | |
$let: { | |
vars: { | |
split_on_CP_str: { | |
$reduce: { | |
input: "$$str_range", | |
initialValue: { | |
split_string: [], | |
index: 0 | |
}, | |
in: { | |
split_string: { | |
$concatArrays: [ | |
"$$value.split_string", | |
[ | |
{ | |
$let: { | |
vars: { | |
the_CP: { | |
$substrCP: ["$a", "$$value.index", 1] | |
} | |
}, | |
in: { | |
$cond: [ | |
{ | |
$eq: [ | |
{ | |
$indexOfCP: [ | |
" \t\r\n", | |
"$$the_CP" | |
] | |
}, | |
-1 | |
] | |
}, | |
{ $add: ["$$value.index", 1] }, | |
0 | |
] | |
} | |
} | |
} | |
] | |
] | |
}, | |
index: { $add: ["$$value.index", 1] } | |
} | |
} | |
} | |
}, | |
in: "$$split_on_CP_str.split_string" | |
} | |
} | |
} | |
} | |
} | |
] | |
}, | |
ltrimmed_a: { | |
$substrCP: [ | |
"$a", | |
{ | |
$min: { | |
$let: { | |
vars: { | |
str_range: { | |
$reverseArray: { | |
$range: [0, { $strLenCP: "$a" }] | |
} | |
} | |
}, | |
in: { | |
$let: { | |
vars: { | |
split_on_CP_str: { | |
$reduce: { | |
input: "$$str_range", | |
initialValue: { | |
split_string: [], | |
index: { $subtract: [{ $size: "$$str_range" }, 1] } | |
}, | |
in: { | |
split_string: { | |
$concatArrays: [ | |
"$$value.split_string", | |
[ | |
{ | |
$let: { | |
vars: { | |
the_CP: { | |
$substrCP: ["$a", "$$value.index", 1] | |
} | |
}, | |
in: { | |
$cond: [ | |
{ | |
$eq: [ | |
{ | |
$indexOfCP: [ | |
" \t\r\n", | |
"$$the_CP" | |
] | |
}, | |
-1 | |
] | |
}, | |
"$$value.index", | |
{ $strLenCP: "$a" } | |
] | |
} | |
} | |
} | |
] | |
] | |
}, | |
index: { $subtract: ["$$value.index", 1] } | |
} | |
} | |
} | |
}, | |
in: "$$split_on_CP_str.split_string" | |
} | |
} | |
} | |
} | |
}, | |
{ $strLenCP: "$a" } | |
] | |
} | |
} | |
}, | |
{ | |
$addFields: { | |
fulltrim: { | |
$cond: [ | |
{ $eq: ["$ltrimmed_a", ""] }, | |
"", | |
{ | |
$substrCP: [ | |
"$ltrimmed_a", | |
0, | |
{ | |
$max: { | |
$let: { | |
vars: { | |
str_range: { | |
$range: [0, { $strLenCP: "$ltrimmed_a" }] | |
} | |
}, | |
in: { | |
$let: { | |
vars: { | |
split_on_CP_str: { | |
$reduce: { | |
input: "$$str_range", | |
initialValue: { | |
split_string: [], | |
index: 0 | |
}, | |
in: { | |
split_string: { | |
$concatArrays: [ | |
"$$value.split_string", | |
[ | |
{ | |
$let: { | |
vars: { | |
the_CP: { | |
$substrCP: [ | |
"$ltrimmed_a", | |
"$$value.index", | |
1 | |
] | |
} | |
}, | |
in: { | |
$cond: [ | |
{ | |
$eq: [ | |
{ | |
$indexOfCP: [ | |
" \t\r\n", | |
"$$the_CP" | |
] | |
}, | |
-1 | |
] | |
}, | |
{ $add: ["$$value.index", 1] }, | |
0 | |
] | |
} | |
} | |
} | |
] | |
] | |
}, | |
index: { $add: ["$$value.index", 1] } | |
} | |
} | |
} | |
}, | |
in: "$$split_on_CP_str.split_string" | |
} | |
} | |
} | |
} | |
} | |
] | |
} | |
] | |
} | |
} | |
} | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment