title: How do i do a Save operation on a MongoDB collection using the Java driver?
body: <p>I just switch over from Python, and need to continue my work with a MongoDB database. One particular task is to save an incoming document (in this case, a tweet) into a collection for archiving. A tweet could comes in multiple times, so I prefer to use <code>save()</code> over <code>insert()</code> since the former do not raise an error if the document already exists in the collection. But it seems the Java driver for MongoDB does not support the save operation. Am I missing something?</p>
  
  <p>EDIT: for reference, i'm using this library <code>'org.mongodb:mongodb-driver:3.0.2'</code></p>
  
  <p>Example code:</p>
  
  <pre><code>MongoCollection&lt;Document&gt; tweets = db.getCollection("tweets");
  ...
  Document tweet = (Document) currentDocument.get("tweet");
  tweets.insertOne(tweet);
  </code></pre>
  
  <p>The last line raise this error when the tweet already exists:</p>
  
  <p><code>Exception in thread "main" com.mongodb.MongoWriteException: insertDocument :: caused by :: 11000 E11000 duplicate key error index: db.tweets.$_id_  dup key: { : ObjectId('55a403b87f030345e84747eb') }</code></p>