title: Official mongo-go-driver findOneAndUpdate using struct
body: <p>I'm creating a simple <code>todo</code> rest API using <code>golang</code> and <code>mongodb</code>. I'm also using the <a href="https://github.com/mongodb/mongo-go-driver" rel="nofollow noreferrer">mongo-go-driver</a> library.</p>
  <p>Basically, I managed to upload my data to <code>mongodb</code> and I'm able to retrieve the information, but I encounter some problems when I try to use the <code>findOneAndUpdate</code> function. I'm able to update my data using <code>findOneAndReplace</code>, However, I want to update the information using a struct and the <code>findOneAndUpdate</code> function</p>
  <p>Here is my struct:</p>
  <pre><code>type Todo struct {
     ObjectId string
     ID       string `json:&quot;ID,omitempty&quot;`
     Title    string `json:&quot;Title,omitempty&quot; bson:&quot;Title&quot;`
     Category string `json:&quot;Category,omitempty&quot; bson:&quot;Category&quot;`
     Content  string `json:&quot;Content,omitempty&quot; bson:&quot;Content&quot;`
     Created  string `json:&quot;Creation_time,omitempty&quot; bson:&quot;Creation_time&quot;`
     Modified string `json:&quot;Modification_time,omitempty&quot; bson:&quot;Modification_time&quot;`
     State    string `json:&quot;State,omitempty&quot; bson:&quot;State&quot;`
  </code></pre>
  <p>}</p>
  <p>Here is the function that decodes my request body into a struct:</p>
  <pre><code>func UpdateTodo (w http.ResponseWriter, r *http.Request){
  var todo models.Todo
  _ = json.NewDecoder(r.Body).Decode(&amp;todo)
  urlTodoId := chi.URLParam(r, &quot;id&quot;)
  UpdateTodoById(todo, urlTodoId)
  </code></pre>
  <p>}</p>
  <p>And finally here is the function that updates the data:</p>
  <pre><code>func UpdateTodoById(todo m.Todo, todoId string) interface{}{
  
  var todoUpdated m.Todo
  
  objId, err := objectid.FromHex(todoId)
  helpers.PanicErr(err)
  filter := bson.NewDocument(bson.EC.ObjectID(&quot;_id&quot;, objId))
  fmt.Println(filter)
  
  err = db.Collection(COLLNAME).FindOneAndUpdate(context.Background(), filter, todo).Decode(&amp;todoUpdated)
  fmt.Println(todoUpdated)
  helpers.PanicErr(err)
  
  return  todoUpdated}
  </code></pre>
  <p>When I send the request I receive an error: <code>update document must contain key beginning with '$</code></p>
  <p>So can anyone help? Is there a way to solve this? and use a struct to update the data find the function <code>findOneAndUpdate</code>? Or is this driver not developed enough to do that yet?</p>
  <p>Thank you.</p>