[GODRIVER-830] Add support for mongodb UUID type Created: 14/Feb/19 Updated: 06/Jul/22 Resolved: 22/Feb/19 |
|
| Status: | Closed |
| Project: | Go Driver |
| Component/s: | BSON |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Steven Ceuppens | Assignee: | Unassigned |
| Resolution: | Won't Fix | Votes: | 1 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||
| Description |
|
Current implementation does not support the mongodb native UUID type, which allows to store and read the golang UUID ([16]byte), as mongodb binary (subtype 4). |
| Comments |
| Comment by Ivari Horm [ 07/Aug/21 ] |
|
Is it still the case? Currently there is an UUID helper class inside the driver: https://github.com/mongodb/mongo-go-driver/tree/master/x/mongo/driver/uuid However, using this generates BSON document with a generic binary subtype (0x00) instead of UUID (0x04) that in return fails to be decoded on subsequent queries. As the UUID type is part of the official BSON specification, it should be appropriate to handle the UUID encoding and decoding at the driver level. Even the driver itself has appropriate subtype values defined: If the MongoDB documentation itself says that UUID is one of the common options to be used as a primary key, it should be supported natively by the driver: https://docs.mongodb.com/manual/core/document/#the-_id-field
|
| Comment by Shawn Looker [ 01/Mar/19 ] |
|
Hi kris.brandow, Sorry for all the questions, but if i'm querying like this: var result SomeStruct{} err = client.Database(databaseName).Collection(collection).FindOne(ctx, bson.M{"_id": assetID}).Decode(&result) How can I test the result. Is there a different way to query that I'm not using that I can check the _id afterwards to figure out whether I need to decode it or not? If you can point me to some documentation that explains any of this, I'd love to read that and not bug you so much.
|
| Comment by Kristofer Brandow (Inactive) [ 22/Feb/19 ] |
|
You should be able to take the bytes from primitive.Binary.Data and turn them into a UUID type of your choice. |
| Comment by Shawn Looker [ 22/Feb/19 ] |
|
Hi kris.brandow, Good to know. I guess I'm unclear how to decode it from a BSON Binary type. I looked in the docs and was unable to find a good example. In our setup, all _id's are going to be UUIDs. I just don't know how to decode them from BSON binary. Shawn |
| Comment by Kristofer Brandow (Inactive) [ 22/Feb/19 ] |
|
Hi slooker, You should be able to check the type of the _id field, if it's 0x05 then it's a BSON Binary type (so UUID) and if it's 0x07 it's a BSON ObjectID. --Kris |
| Comment by Shawn Looker [ 22/Feb/19 ] |
|
Hi kris.brandow, With the current implementation, how can we query if _id is set to be a UUID instead of an ObjectID? Thanks, Shawn |
| Comment by Kristofer Brandow (Inactive) [ 19/Feb/19 ] |
|
Hi stevenceuppens, In MongoDB drivers we don't attempt to interpret the bytes in a BSON Binary value. If you want this functionality, registering a codec or creating a UUID type with the proper Marshal* and Unmarshal* methods, should do the trick. Thanks, Kris |
| Comment by Shawn Looker [ 19/Feb/19 ] |
|
About how long does it normally take a ticket to be approved and merged in, assuming there's no issues? |
| Comment by Steven Ceuppens [ 15/Feb/19 ] |
|
PR created https://review.gerrithub.io/c/mongodb/mongo-go-driver/+/444886
|