title: Invoke db.eval in FindAndModify using MongoDB C# Client
body: <p>I have the following Document:</p>
  
  <pre><code>{
  "_id": 100,
  "Version": 1,
  "Data": "Hello"
  }
  </code></pre>
  
  <p>I have a function which return a number from a sequence:</p>
  
  <pre><code>function getNextSequence(name) {
     var ret = db.Counter.findAndModify(
            {
              query: { _id: name },
              update: { $inc: { value: 1 } },
              new: true,
              upsert: true
            }
     );
  
     return ret.value;
  }
  </code></pre>
  
  <p>I can use this for optimistic concurrency by performing the following Mongo command:</p>
  
  <pre><code>db.CollectionName.findAndModify({
      query: { "_id" : NumberLong(100), "Version" : 1 },
      update: { "$set" : { 
           "Data": "Here is new data!",
           "Version" : db.eval('getNextSequence("CollectionName")') } 
      },
      new: true
      }
  );
  </code></pre>
  
  <p>This will update the document (as the _id and Version) match, with the new Data field, and also the new number out of the eval call.</p>
  
  <p>It also returns a modified document, from which I can retrieve the new Version value if I want to make another update later (in the same 'session').</p>
  
  <p><strong>My problem is:</strong></p>
  
  <p>You cannot create an Update document using the MongoDB C# client that will serialize to this command.</p>
  
  <p>I used: </p>
  
  <pre><code> var update = Update.Combine(
                  new UpdateDocument("$set", doc),
                  Update.Set(versionMap.ElementName, new BsonJavaScript("db.eval('getNextSequence(\"Version:CollectionName\")')")))
                  );
  </code></pre>
  
  <p>If you use what I first expected to perform this task, BsonJavascript, you get the following document, which incorrectly sets Version to a string of javascript.</p>
  
  <pre><code>update: { "$set" : { 
           "Data": "Here is new data!",
           "Version" : { "$code" : "db.eval('getNextSequence(\"Version:CollectionName\")')" }
             }
   }
  </code></pre>
  
  <p><strong>How can I get MongoDB C# client to serialize an Update document with my db.eval function call in it?</strong></p>
  
  <p>I have tried to add a new BsonValue type in my assembly which I would serialize down to db.eval(''); However there is a BsonType enum which I cannot modify, without making a mod to MongoDB which I would not like to do incase of any issues with the change, compatibility etc.</p>
  
  <p>I have also tried simply creating the Update document myself as a BsonDocument, however FindAndModify will only accept an IMongoUpdate interface which a simply a marker that at present I find superfluous.</p>
  
  <p>I have just tried to construct the command manually by creating a BsonDocument myself to set the Value: db.eval, however I get the following exception:
  A String value cannot be written to the root level of a BSON document.</p>
  
  <p>I see no other way now than drop down to the Mongo stream level to accomplish this.</p>