MongoDB Map Property "new" in findAndModify using FindOneAndUpdateOptions class C #

I am trying to implement a function getNextSequence

for mongoDB to explain this Link I am using C # latte driver but I am not how to map a property new : true

inFindOneAndUpdateOptions

MongoDB Code

function getNextSequence(name) {
   var ret = db.counters.findAndModify(
          {
            query: { _id: name },
            update: { $inc: { seq: 1 } },
            new: true,
            upsert: true
          }
   );

   return ret.seq;
}

      

C # code

    public async Task<long> GetNextObjectSequenceAsync(string objectName)
    {
        var collection = this.Context.GetCollection<ObjectSequence>("Counters");
        var filter = new FilterDefinitionBuilder<ObjectSequence>().Where(x => x.Name == objectName);
        var options = new FindOneAndUpdateOptions<ObjectSequence, ObjectSequence>() { IsUpsert = true };
        var update = new UpdateDefinitionBuilder<ObjectSequence>().Inc(x => x.Sequence, 1);

        ObjectSequence seq = await collection.FindOneAndUpdateAsync<ObjectSequence>(filter, update, options);

        return seq.Sequence;

    }

      

+3


source to share


1 answer


FindOneAndUpdateOptions

has an ReturnDocument

enumeration, where

ReturnDocument.Before = 'new': false

ReturnDocument.After = 'new': true



In your case, the parameters should be:

var options = new FindOneAndUpdateOptions<ObjectSequence, ObjectSequence>() { ReturnDocument = ReturnDocument.After, IsUpsert = true };

      

+3


source







All Articles