英文:
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.
The stranger thing is that Metabase displays the ID correctly, but I cannot manipulate this data because it’s not in string format.
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([
{
"$project": {
"_idUUIDstr": {
"$function": {
"body": "function(x) {return x.toString().slice(6,-2)}",
"args": ["$_id"],
"lang": "js"
}
}
}
}
])
Example document:
{
"_id": BinData(4, "OyQRAeK7QlWMr0E2xWapYg==")
}
Example output:
{
"_id": BinData(4, "OyQRAeK7QlWMr0E2xWapYg=="),
"_idUUIDstr": "3b241101-e2bb-4255-8caf-4136c566a962"
}
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
{
"_id": BinData(3, "OyQRAeK7QlWMr0E2xWapYg==")
}
I refactor your answer as follow :
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"
}
}
}
}
])
and is work perfectly
RESULT
[
{
"_id": BinData(3, "OyQRAeK7QlWMr0E2xWapYg=="),
"_idUUIDstr": "0111243b-bbe2-5542-8caf-4136c566a962"
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论