mongo go driver: uint always marshals to bson int64

The documentation for the bson package states the following:

uint, uint32, and uint64 marshal to a BSON int32 if the value is between math.MinInt32 and math.MaxInt32, inclusive, and BSON int64 otherwise.

But for some reason, uint always marshals into bson int64. On the other hand int behaves as excepted.

Here you can find a working example of the problem. It seems like a really simple thing I am probably missing, so I am currently not opening an issue.

  • If I remember correctly, mongodb does not support uint natively

    – 

  • bson indeed doesn’t have uint, but the go driver should still marshal go uint to the correct bson type.

    – 




  • 1

    what do you think should be correct type?

    – 

  • As I mentioned in the question, the documentation states: “uint, uint32, and uint64 marshal to a BSON int32 if the value is between math.MinInt32 and math.MaxInt32, inclusive, and BSON int64 otherwise.” Was just wondering why it doesn’t behave according to the docs.

    – 

  • There appears to be an option to encode integers as their minimum size types which the entry point you are using doesn’t set: pkg.go.dev/go.mongodb.org/mongo-driver/bson#Encoder.IntMinSize

    – 

Turns out the documentation was indeed wrong. This issue was created in response to my question.

Since it seems the documentation will be updated, I guess this question can be closed if moderators think it is no longer necessary.

Leave a Comment