MongoDB : Remove last two characters from String

删除回忆录丶 提交于 2021-02-07 19:14:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!