MongoDB:从字符串中删除最后两个字符 [英] MongoDB : Remove last two characters from String

查看:69
本文介绍了MongoDB:从字符串中删除最后两个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何去除MongoDB中String的最后两个字符?

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 ]

推荐答案

你必须使用 $add$subrtract 评估新字符串的长度:

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 游乐场

这篇关于MongoDB:从字符串中删除最后两个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆