[GODRIVER-860] Document using UUIDs in mongo as _id's. Created: 04/Mar/19  Updated: 08/Aug/22  Resolved: 08/Aug/22

Status: Closed
Project: Go Driver
Component/s: Documentation
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Minor - P4
Reporter: Shawn Looker Assignee: Benji Rewis (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to GODRIVER-2484 Add a Go type for the UUID BSON binar... Backlog

 Description   

I've been told that there's already support for using mongo's UUID as an _id, but I can't figure out how to do it, and I've not been able to find any documentation on it.  If there's a way to do it, can we please detail it in the README for mongo-go-driver?



 Comments   
Comment by Benji Rewis (Inactive) [ 08/Aug/22 ]

Feel free to follow up on this ticket if you have further questions or on GODRIVER-2484 if you're curious about the new UUID type! I've added a comment to GODRIVER-2484 to make sure we document the new type's usage alongside primitive.ObjectID.

Comment by Benji Rewis (Inactive) [ 08/Aug/22 ]

Hello again slooker ! Just wanted to follow up on this ticket with a bit more information; apologies for the rather extreme delay haha.

I've been told that there's already support for using mongo's UUID as an _id

MongoDB uses "object IDs" as the type for fields like "id". While the creation of object IDs and UUIDs differ, their use-case is basically the same (to "uniquely" represent an object). Internally, the Go driver will automatically generate a primitive.ObjectID for the _id field if the _id field is not provided. primitive.ObjectID is encoded as BSON type 0x07. There _is support for object IDs with primitive.ObjectID. If your assetID is just being used to represent the Mongo document's ID, SomeStruct could just have an id field of type primitive.ObjectID (with a struct tag bson:"_id").

If, however, assetID is a UUID type like google.UUID that is generated somewhere else in your application, it may be more difficult to use with MongoDB and the Go driver. Encoding and decoding of external UUID types is generally done with 0x05 (BSON binary type) or 0x00 (generic binary). There is currently no type in the Go driver to represent UUIDs but we're planning to add one with GODRIVER-2484.

Comment by Shawn Looker [ 04/Mar/19 ]

kris.brandow said you can do it like this:

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.

But if I'm querying like this, I have no idea how you would use a UUID to query,  or what sort of field you'd need to define in your struct for the _id field

var result SomeStruct{}

err = client.Database(databaseName).Collection(collection).FindOne(ctx, bson.M{"_id": assetID}).Decode(&result)

Generated at Thu Feb 08 08:35:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.