【发布时间】:2019-10-25 11:38:15
【问题描述】:
MongoDB中如何去掉String的最后两个字符?
我尝试了以下解决方案,但没有成功。
$substr: [ "$document.field", 0, { $strLenCP: "$document.field" } - 2 ]
【问题讨论】:
MongoDB中如何去掉String的最后两个字符?
我尝试了以下解决方案,但没有成功。
$substr: [ "$document.field", 0, { $strLenCP: "$document.field" } - 2 ]
【问题讨论】:
您必须使用$add 或$subrtract 来评估新字符串的长度:
db.collection.aggregate([
{
$project: {
newStr: {
$substr: [ "$document.field", 0, { $add: [ { $strLenCP: "$document.field" }, -2 ] } ]
}
}
}
])
【讨论】: