title: Add field with value to existing document in MongoDB via Java API
body: <p>The following code haven't worked for me:</p>
  
  <pre><code>public void addFieldWithValueToDoc(String DBName, String collName, String docID, String key, String value) {
      BasicDBObject setNewFieldQuery = new BasicDBObject().append("$set", new BasicDBObject().append(key, value));
      mongoClient.getDB(DBName).getCollection(collName).update(new BasicDBObject().append("_id", docID), setNewFieldQuery);
  }
  </code></pre>
  
  <p>Where mongoClient variable's type is MongoClient.</p>
  
  <p>It's inspired by <a href="https://stackoverflow.com/questions/7714216/add-new-field-to-a-collection-in-mongodb">Add new field to a collection in MongoDB</a> .
  What's wrong and how to do it right?
  Thanks.</p>