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

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

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表示。直到有更好的东西出现,也许这已经足够了。"

  1. db.collection.aggregate([
  2. {
  3. "$project": {
  4. "_idUUIDstr": {
  5. "$function": {
  6. "body": "function(x) {return x.toString().slice(6,-2)}",
  7. "args": ["$_id"],
  8. "lang": "js"
  9. }
  10. }
  11. }
  12. }
  13. ])

示例文档:

  1. {
  2. "_id": BinData(4, "OyQRAeK7QlWMr0E2xWapYg==")
  3. }

示例输出:

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

mongoplayground.net上尝试它。

  1. <details>
  2. <summary>英文:</summary>
  3. 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_.
  4. ```mongodb
  5. db.collection.aggregate([
  6. {
  7. &quot;$project&quot;: {
  8. &quot;_idUUIDstr&quot;: {
  9. &quot;$function&quot;: {
  10. &quot;body&quot;: &quot;function(x) {return x.toString().slice(6,-2)}&quot;,
  11. &quot;args&quot;: [&quot;$_id&quot;],
  12. &quot;lang&quot;: &quot;js&quot;
  13. }
  14. }
  15. }
  16. }
  17. ])

Example document:

  1. {
  2. &quot;_id&quot;: BinData(4, &quot;OyQRAeK7QlWMr0E2xWapYg==&quot;)
  3. }

Example output:

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

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

答案2

得分: 0

感谢您的帮助!

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

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

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

  1. db.collection.aggregate([
  2. {
  3. "$project": {
  4. "_idUUIDstr": {
  5. "$function": {
  6. "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)}",
  7. "args": [
  8. "$_id"
  9. ],
  10. "lang": "js"
  11. }
  12. }
  13. }
  14. }
  15. ])

它运行得很完美

结果

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

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

英文:

@rickhg12hs

Thank you a lot, your answer help me !

But in my case my Id was BinData 3

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

I refactor your answer as follow :

  1. db.collection.aggregate([
  2. {
  3. &quot;$project&quot;: {
  4. &quot;_idUUIDstr&quot;: {
  5. &quot;$function&quot;: {
  6. &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;,
  7. &quot;args&quot;: [
  8. &quot;$_id&quot;
  9. ],
  10. &quot;lang&quot;: &quot;js&quot;
  11. }
  12. }
  13. }
  14. }
  15. ])

and is work perfectly

RESULT

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

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:

确定