title: How to add values to an bson.D object
body: <p>I'm working with golang and the MongoDB driver, I want to patch one of my objects according to the data I get from the outside:</p>
  
  <p>I have a struct:</p>
  
  <pre><code>type Pivot struct {
      Email        string             `json:"email"`
      Base         string             `json:"base"`
  }
  </code></pre>
  
  <p>And the patch (with MongoDB Update)</p>
  
  <pre><code>setMap := bson.D{
          {"$set", setElements},
      }
  
  res, err := collection.UpdateMany(
      ctx,
      filter,
      setMap,
  )
  </code></pre>
  
  <p>And I want to make the setObject a little bit dynamic:</p>
  
  <pre><code>if len(pivot.Base) &gt; 0 {
    setElements.append("base", pivot.Base) //this doesn't work...
  }
  if len(pivot.Email) &gt; 0 {
    setElements.append("email", pivot.Email)
  }
  </code></pre>
  
  <p>I' ve seen that the setObject <a href="https://github.com/mongodb/mongo-go-driver/blob/master/examples/documentation_examples/examples.go#L1517" rel="nofollow noreferrer">can be built like</a></p>
  
  <pre><code>{"$set", bson.D{
      {"processed", pivot.Processed},
  }
  </code></pre>
  
  <p>But how can I make it dynamic?</p>