title: How to store a UUID in MongoDB with Golang?
body: <p>When storing a <code>github.com/google/uuid</code> UUID field in MongoDB using Golang it translates to a base64 binary with subtype 0. This makes it impossible to naturally query the document field by UUID.</p>
  <p>The inserted user looks like:</p>
  <pre class="lang-json prettyprint-override"><code>{&quot;_id&quot;:{&quot;$binary&quot;:&quot;0bHYoNWSTV+KqWSl54YWiQ==&quot;,&quot;$type&quot;:&quot;0&quot;},&quot;name&quot;:&quot;Isabella&quot;}
  </code></pre>
  <p>When querying by the generated UUID <code>d1b1d8a0-d592-4d5f-8aa9-64a5e7861689</code>, the results are empty.</p>
  <pre class="lang-golang prettyprint-override"><code>type User struct {
      UserId uuid.UUID `json:&quot;userId&quot; bson:&quot;_id&quot;`
      Name   string    `json:&quot;name&quot; bson:&quot;name&quot;`
  }
  
  func (repo userRepo) User(uuidIn uuid.UUID) (model.User, error) {
      collection := repo.database.Collection(mongoCollectionUser)
      var user model.User
      err := collection.FindOne(context.Background(),
          bson.M{&quot;_id&quot;: uuidIn},
      ).Decode(&amp;user)
      // err: mongo: no documents in result
  }
  </code></pre>