[GODRIVER-1111] type conversion like mgo Created: 05/Jun/19  Updated: 07/Jun/19  Resolved: 07/Jun/19

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

Type: Improvement Priority: Major - P3
Reporter: Pierre Durand Assignee: Divjot Arora (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

With the old mgo library, type conversion were handled automatically.

I have 2 issues while migrating an application:

  • MongoDB field is a number, Go struct field is a string
  • MongoDB field contains null, Go struct field is a string

The BSON library returns an error, because the types don't match.

I understand that the mapping should be strict, and personally I think it's a good practice.

However I don't have full control over the data managed by my company. Some developers use programming language with poor typing (JS), and they don't care about the type of data they insert.

With the old mgo library, my code was working fine, because it was doing the type conversion for me. See https://github.com/globalsign/mgo/blob/eeefdecb41b842af6dc652aaea4026e8403e62df/bson/decode.go#L820-L829

I understand if you don't want to change the code of the official MongoDB driver.



 Comments   
Comment by Divjot Arora (Inactive) [ 07/Jun/19 ]

pierrre Thank you for the BSON suggestions. This behavior can be achieved with a custom registry (seems like you'd want a custom codec to unmarshal strings specifically for the issues you listed). We do have plans to create a set of codecs to provide mgo-compatibility later this quarter. You can track https://jira.mongodb.org/browse/GODRIVER-226 for that work.

Comment by Pierre Durand [ 06/Jun/19 ]

After some investigation, I think that I can implement the old behavior with a custom registry. 

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