MongoDB聚合 – 如何在字段左侧填充零和字母?

huangapple go评论63阅读模式
英文:

MongoDB Aggregation - How can I pad an field with zeros and letters on the left?

问题

我想要以下的结果:

[
  {
    "id": "63f7aad063710fe0106dbc04",
    "name": "Kristen Welch",
    "address": "267 Dooley Street, Westphalia, New York, 1648",
    "trackingID": "TR0000000963XP",
    "greeting": "Hello, Kristen Welch! You have 9 unread messages.",
    "favoriteFruit": "apple"
  },
  {
    "id": "63f7aad0c156afad133a66b6",
    "name": "Shaw Roach",
    "address": "254 Newkirk Placez, Hiseville, American Samoa, 7644",
    "trackingID": "TR0000000729XP",
    "greeting": "Hello, Shaw Roach! You have 7 unread messages.",
    "favoriteFruit": "banana"
  }
]
英文:

Can you help me with a situation... I Have this Json but I would like to format the field trackingID like "TR000000012345BR". How can I pad a trackingId with letters and zeros on the left and letter on the rigth by aggregate?

[
  {
    "_id": "63f7aad063710fe0106dbc04",
    "name": "Kristen Welch",
    "address": "267 Dooley Street, Westphalia, New York, 1648",
    "trackingID": 963,
    "greeting": "Hello, Kristen Welch! You have 9 unread messages.",
    "favoriteFruit": "apple"
  },
  {
    "_id": "63f7aad0c156afad133a66b6",
    "name": "Shaw Roach",
    "address": "254 Newkirk Placez, Hiseville, American Samoa, 7644",
    "trackingID": 729,
    "greeting": "Hello, Shaw Roach! You have 7 unread messages.",
    "favoriteFruit": "banana"
  }
]

I would like this result below:

[
  {
    "_id": "63f7aad063710fe0106dbc04",
    "name": "Kristen Welch",
    "address": "267 Dooley Street, Westphalia, New York, 1648",
    "trackingID": TR0000000963XP,
    "greeting": "Hello, Kristen Welch! You have 9 unread messages.",
    "favoriteFruit": "apple"
  },
  {
    "_id": "63f7aad0c156afad133a66b6",
    "name": "Shaw Roach",
    "address": "254 Newkirk Placez, Hiseville, American Samoa, 7644",
    "trackingID": TR0000000729XP,
    "greeting": "Hello, Shaw Roach! You have 7 unread messages.",
    "favoriteFruit": "banana"
  }
]

<https://mongoplayground.net/p/8uUd7DVcW9R>

答案1

得分: 2

db.collection.aggregate([
  {
    $set: {
      "trackIngID": {
        $concat: [									
          "TR",
          {
            "$substrCP": [							
              {
                $toLong: {							
                  $add: [10000000000,"$trackingID"]
                }
              },
              1,
              10
            ]
          },
          "XP"
        ]
      }
    }
  }
])

演示链接

英文:
db.collection.aggregate([
  {
    $set: {
      &quot;trackIngID&quot;: {
        $concat: [									//4. concatenate prefix and suffix
          &quot;TR&quot;,
          {
            &quot;$substrCP&quot;: [							//3. remove leading 1
              {
                $toLong: {							//2. convert to long
                  $add: [10000000000,&quot;$trackingID&quot;] //1. add 10000000000, to pad it with zeros
                }
              },
              1,
              10
            ]
          },
          &quot;XP&quot;
        ]
      }
    }
  }
])

Demo

答案2

得分: 0

在聚合管道中,你可以执行以下操作:

  1. 使用 $concat 在字符串的开头添加 9 个零。
  2. 使用 $substrCP 剪切掉额外的前导零。使用 $strLenCP$subtract 来计算偏移量。
  3. 使用 $concat 添加前缀 'TR' 和后缀 'XP'。
db.collection.aggregate([
  {
    "$set": {
      "trackingID": {
        // 使用 9 个零进行填充
        "$concat": [
          "000000000",
          {
            $toString: "$trackingID"
          }
        ]
      }
    }
  },
  {
    "$set": {
      "trackingID": {
        // 剪切掉额外的前导零
        "$substrCP": [
          "$trackingID",
          {
            "$subtract": [
              {
                "$strLenCP": "$trackingID"
              },
              9
            ]
          },
          9
        ]
      }
    }
  },
  {
    "$set": {
      "trackingID": {
        "$concat": [
          "TR",
          "$trackingID",
          "XP"
        ]
      }
    }
  }
])

Mongo Playground

英文:

You can do the followings in an aggregation pipeline:

  1. pad with 9 zeros at the start of the string with $concat
  2. trim the extra leading zeros with $substrCP. Use $strLenCP and $subtract to calculate the offset.
  3. $concat with the prefix 'TR' and suffix 'XP'
db.collection.aggregate([
  {
    &quot;$set&quot;: {
      &quot;trackingID&quot;: {
        // pad with 9 zeros
        &quot;$concat&quot;: [
          &quot;000000000&quot;,
          {
            $toString: &quot;$trackingID&quot;
          }
        ]
      }
    }
  },
  {
    &quot;$set&quot;: {
      &quot;trackingID&quot;: {
        // trim the extras leading zeros
        &quot;$substrCP&quot;: [
          &quot;$trackingID&quot;,
          {
            &quot;$subtract&quot;: [
              {
                &quot;$strLenCP&quot;: &quot;$trackingID&quot;
              },
              9
            ]
          },
          9
        ]
      }
    }
  },
  {
    &quot;$set&quot;: {
      &quot;trackingID&quot;: {
        &quot;$concat&quot;: [
          &quot;TR&quot;,
          &quot;$trackingID&quot;,
          &quot;XP&quot;
        ]
      }
    }
  }
])

Mongo Playground

huangapple
  • 本文由 发表于 2023年2月24日 02:32:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定