MongoDB,将UUID转换为字符串,在投影中

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

MongoDb, Convert UUID to string, in projection

问题

我正在寻找一种使用投影将UUID转换为字符串的解决方案。我尝试了许多不同的方法,但没有一种方法有效。

奇怪的是,Metabase正确显示了ID,但我无法操作这些数据,因为它不是字符串格式。

你有任何想法吗?

非常感谢!

本杰明

英文:

I’m looking for a solution to convert a UUID to a string using projection. I’ve tried many different ways, but none of them are working.

MongoDB,将UUID转换为字符串,在投影中

The stranger thing is that Metabase displays the ID correctly, but I cannot manipulate this data because it’s not in string format.

MongoDB,将UUID转换为字符串,在投影中
Have you any idea ?

Thanks a lot

Benjamin

答案1

得分: 0

"我希望有一个更好的答案,因为我认为这非常脆弱,因为它依赖于MongoDB服务器上启用JavaScript执行以及BinData UUID的固定toString表示。直到有更好的东西出现,也许这已经足够了。"

db.collection.aggregate([
  {
    "$project": {
      "_idUUIDstr": {
        "$function": {
          "body": "function(x) {return x.toString().slice(6,-2)}",
          "args": ["$_id"],
          "lang": "js"
        }
      }
    }
  }
])

示例文档:

{
  "_id": BinData(4, "OyQRAeK7QlWMr0E2xWapYg==")
}

示例输出:

{
  "_id": BinData(4, "OyQRAeK7QlWMr0E2xWapYg=="),
  "_idUUIDstr": "3b241101-e2bb-4255-8caf-4136c566a962"
}

mongoplayground.net上尝试它。


<details>
<summary>英文:</summary>

I hope there is a better answer as I consider this very fragile since it depends on JavaScript execution being enabled on the MongoDB server and a fixed `toString` representation of `BinData` UUID.  Until there is something better, perhaps this is _good enough_.
```mongodb
db.collection.aggregate([
  {
    &quot;$project&quot;: {
      &quot;_idUUIDstr&quot;: {
        &quot;$function&quot;: {
          &quot;body&quot;: &quot;function(x) {return x.toString().slice(6,-2)}&quot;,
          &quot;args&quot;: [&quot;$_id&quot;],
          &quot;lang&quot;: &quot;js&quot;
        }
      }
    }
  }
])

Example document:

{
  &quot;_id&quot;: BinData(4, &quot;OyQRAeK7QlWMr0E2xWapYg==&quot;)
}

Example output:

{
  &quot;_id&quot;: BinData(4, &quot;OyQRAeK7QlWMr0E2xWapYg==&quot;),
  &quot;_idUUIDstr&quot;: &quot;3b241101-e2bb-4255-8caf-4136c566a962&quot;
}

Try it on [mongoplayground.net](https://mongoplayground.net/p/0S3YJodtDVB "Click me!").

答案2

得分: 0

感谢您的帮助!

但在我的情况下,我的ID是 BinData 3

{
  "_id": BinData(3, "OyQRAeK7QlWMr0E2xWapYg==")
}

我根据您的回答进行了重构:

db.collection.aggregate([
  {
    "$project": {
      "_idUUIDstr": {
        "$function": {
          "body": "function convert(s){let t=s.hex();var r=t.substr(6,2)+t.substr(4,2)+t.substr(2,2)+t.substr(0,2),u=t.substr(10,2)+t.substr(8,2),b=t.substr(14,2)+t.substr(12,2),e=t.substr(16,16);return t=r+u+b+e,t.substr(0,8)+'-'+t.substr(8,4)+'-'+t.substr(12,4)+'-'+t.substr(16,4)+'-'+t.substr(20,12)}",
          "args": [
            "$_id"
          ],
          "lang": "js"
        }
      }
    }
  }
])

它运行得很完美

结果

[
  {
    "_id": BinData(3, "OyQRAeK7QlWMr0E2xWapYg=="),
    "_idUUIDstr": "0111243b-bbe2-5542-8caf-4136c566a962"
  }
]

链接:https://mongoplayground.net/p/qf7e5d2aA41

英文:

@rickhg12hs

Thank you a lot, your answer help me !

But in my case my Id was BinData 3

{
  &quot;_id&quot;: BinData(3, &quot;OyQRAeK7QlWMr0E2xWapYg==&quot;)
}

I refactor your answer as follow :

db.collection.aggregate([
  {
    &quot;$project&quot;: {
      &quot;_idUUIDstr&quot;: {
        &quot;$function&quot;: {
          &quot;body&quot;: &quot;function convert(s){let t=s.hex();var r=t.substr(6,2)+t.substr(4,2)+t.substr(2,2)+t.substr(0,2),u=t.substr(10,2)+t.substr(8,2),b=t.substr(14,2)+t.substr(12,2),e=t.substr(16,16);return t=r+u+b+e,t.substr(0,8)+&#39;-&#39;+t.substr(8,4)+&#39;-&#39;+t.substr(12,4)+&#39;-&#39;+t.substr(16,4)+&#39;-&#39;+t.substr(20,12)}&quot;,
          &quot;args&quot;: [
            &quot;$_id&quot;
          ],
          &quot;lang&quot;: &quot;js&quot;
        }
      }
    }
  }
])

and is work perfectly

RESULT

[
  {
    &quot;_id&quot;: BinData(3, &quot;OyQRAeK7QlWMr0E2xWapYg==&quot;),
    &quot;_idUUIDstr&quot;: &quot;0111243b-bbe2-5542-8caf-4136c566a962&quot;
  }
]

https://mongoplayground.net/p/qf7e5d2aA41

huangapple
  • 本文由 发表于 2023年7月31日 21:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804105.html
匿名

发表评论

匿名网友

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

确定