FindOneAndUpdateOptions

FindOneAndUpdateOptions

我正在尝试为MongoDB实现一个getNextSequence函数explain on thisLink我正在使用Lattes C驱动程序,但我不确定如何映射new : true中的FindOneAndUpdateOptions属性
MongoDB代码

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

   return ret.seq;
}

C代码
    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;

    }

最佳答案

FindOneAndUpdateOptions具有ReturnDocument枚举,其中
returndocument.before='新建':false
returndocument.after='新建':真
在您的情况下,选项应该是:

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

10-07 23:55