How to get _id after updating a MongoDB document?
<p>I am using <a href="https://docs.mongodb.org/ecosystem/drivers/csharp/" rel="nofollow">.Net MongoDB.Driver 2.2.3</a> and MongoDB 3.2. I want to get the <code>_id</code> of the document I insert or replace with <code>collection.ReplaceOneAsync()</code>, because I use it later on.</p>

<p>However the <code>UpsertedId</code> of the result is <code>null</code> if the operation was an <em>update</em> (it is set, if an <em>insert</em> happened).</p>

<p>What is the best way to determine the <code>_id</code> of the updated document?</p>

<pre class="lang-cs prettyprint-override"><code>var collection = GetMongoDatabase().GetCollection&lt;BsonDocument&gt;("foos");
var document = new BsonDocument
{
     {"foo", "new bar"}
};
var result = await collection.ReplaceOneAsync(
    Builders&lt;BsonDocument&gt;.Filter.Eq("foo", "bar"),
    document,
    new UpdateOptions {IsUpsert = true});
// I want to use result.UpsertedId here;
</code></pre>

<p>If I take a look at <code>result</code> after an update in Visual Studio's Immediate Window, I see:</p>

<pre><code>?result
{MongoDB.Driver.ReplaceOneResult.Acknowledged}
    [MongoDB.Driver.ReplaceOneResult.Acknowledged]: {MongoDB.Driver.ReplaceOneResult.Acknowledged}
    IsAcknowledged: true
    IsModifiedCountAvailable: true
    MatchedCount: 1
    ModifiedCount: 1
    UpsertedId: null
</code></pre>