[GODRIVER-938] cannot convert types that use mgobson.ObjectId to new bson library. Created: 08/Apr/19  Updated: 28/Oct/23  Resolved: 15/Jan/20

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

Type: Task Priority: Major - P3
Reporter: Sam Kleinman (Inactive) Assignee: Isabella Siu (Inactive)
Resolution: Fixed Votes: 1
Labels: evg, mgocompat
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to GODRIVER-1216 mgo compatible bson registry Closed
is related to GODRIVER-953 way to force decoding ints into string Closed
Epic Link: mgo migration tool

 Description   

We currently have a number of types which have _id fields that are specified as strings, but take advantage of the fact that these will safely old mgobson's objectid. The hardest part of this, is that we use these structs in some situation where we specify our own ID value as a string)

In order to migrate to the new bson library we'll need some way of handling these OID types.



 Comments   
Comment by Isabella Siu (Inactive) [ 15/Jan/20 ]

With changes for mgocompat, ObjectIDs will decode to strings. To have them decode like mgo's, use the mgocompat.MgoRegistry.

Comment by Sam Kleinman (Inactive) [ 11/Apr/19 ]

I mean, I think the trivial example should (mostly) work once you create session/client objects.

The problem is that when you insert an empty document, MGO creates an object ID, and you could read it back with mgo because objectids are strings.

If did a query in mongo-driver using this structure, it would error because the type of the ID field wouldn't be correct.

Comment by Kristofer Brandow (Inactive) [ 11/Apr/19 ]

sam.kleinman, if I understand correctly, when you insert an example instance with the ID field unset, then mgo would create an ObjectID for it. What happens if you read one of these objects and then write it back to the database? From what we can surmise mgo would attempt to write the _id field into the database as a BSON String, which would be invalid because the _id is immutable.

Comment by Sam Kleinman (Inactive) [ 08/Apr/19 ]

the minimal example is this:

type example struct{ 
    ID string `bson:"_id,omitempty"`
}
ex := &example{}
mgo.Session().DB("test").C("test").Insert(ex)
err := mongo.Client.Database("test").Collection("test").FindOne(nil).Decode(ex)

It definitely happens for this type: https://github.com/evergreen-ci/evergreen/blob/master/model/task/task.go#L54

Comment by Ian Whalen (Inactive) [ 08/Apr/19 ]

sam.kleinman can you give us an example struct that worked with mgo and any example usage that would help clarify?

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