Golang Hints: Create MongoDB Object ID from String

Sometimes you may want to find something by the Object ID in your MongoDB and due to you URI or query string contains ID you should convert the string ID to Object ID.

Below is a code that converts string ID to Object ID:

 package main

import (
func main() {
oid, err := primitive.ObjectIDFromHex(recordID)

fmt.Printf("%s %v", err, oid)

  // if err := collection.FindOne(ctx, bson.M{"_id": oid}).Decode(e); err != nil {
  // return nil, err
// }