问题
How to remove the last two characters of String in MongoDB?
I have tried below solution but it did not work.
$substr: [ "$document.field", 0, { $strLenCP: "$document.field" } - 2 ]
回答1:
You have to use $add or $subrtract to evaluate the length of a new string:
db.collection.aggregate([
{
$project: {
newStr: {
$substr: [ "$document.field", 0, { $add: [ { $strLenCP: "$document.field" }, -2 ] } ]
}
}
}
])
MongoDB Playground
来源:https://stackoverflow.com/questions/56537076/mongodb-remove-last-two-characters-from-string