【问题标题】:MongoDB : Remove last two characters from StringMongoDB:从字符串中删除最后两个字符
【发布时间】:2019-10-25 11:38:15
【问题描述】:

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

我尝试了以下解决方案,但没有成功。

   $substr: [ "$document.field", 0, { $strLenCP: "$document.field" } - 2 ]

【问题讨论】:

    标签: mongodb substring substr


    【解决方案1】:

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

    db.collection.aggregate([
        {
            $project: {
                newStr: {
                    $substr: [ "$document.field", 0, { $add: [ { $strLenCP: "$document.field" }, -2 ] } ]
                }
            }
        }
    ])
    

    MongoDB Playground

    【讨论】:

      猜你喜欢
      • 2016-01-21
      • 2017-02-22
      • 2017-08-19
      • 2020-03-18
      • 1970-01-01
      • 2015-08-22
      相关资源
      最近更新 更多