[GODRIVER-323] Struct with bson tag _id inserts a byte array instead of objectId Created: 03/Apr/18  Updated: 28/Oct/23  Resolved: 06/Apr/18

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

Type: Bug Priority: Major - P3
Reporter: Ad Assignee: Kristofer Brandow (Inactive)
Resolution: Fixed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File Screen Shot 2018-04-03 at 15.09.21.png    
Issue Links:
Problem/Incident
is caused by GODRIVER-324 Handle objectid.ObjectID in the bson.... Closed

 Description   

When having a struct like this:

type TestStruct struct {
        ID   objectid.ObjectID `bson:"_id"`
	Desc string            `bson:"desc"`
}
 
func Create() {
       t := TestStruct{
              Desc: "Testing 1234"
       }
 
       .....InsertOne(context.Background(), t, nil)
}

This creates a byte[12] (Binary) value in the database instead of an objectID. When removing the `bson: "_id"` tag. It automatically creates a correct objectID. But then there is no bson mapping to the ID field in the struct.



 Comments   
Comment by Kristofer Brandow (Inactive) [ 06/Apr/18 ]

No problem, thanks for the bug report!

Comment by Ad [ 06/Apr/18 ]

Did a quick test and I can confirm that it works.
Thanks!

Comment by Kristofer Brandow (Inactive) [ 05/Apr/18 ]

Hi Ad,

I've just merged a commit that should fix this problem. Please test and let me know if it works for you.

--Kris

Comment by Githook User [ 05/Apr/18 ]

Author:

{'email': 'kris@mongodb.com', 'name': 'Kris Brandow', 'username': 'skriptble'}

Message: Add support for encoding ObjectIDs

Added support for struct fields, map keys, map values, and slice values
that are ObjectIDs.

GODRIVER-324
GODRIVER-323

Change-Id: I070fef2199022ca47dcee3546054e3f6be077ff2
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/8a0b12411980d40543cfb8643272bc60151e0204

Comment by Ad [ 03/Apr/18 ]

Great, thanks!

Comment by Kristofer Brandow (Inactive) [ 03/Apr/18 ]

Hi Ad,

It looks like we aren't actually explicitly checking for an objectid.ObjectID so the type is being interpreted as [12]byte instead. I've added GODRIVER-324 to fix this.

--Kris

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