简体   繁体   English

MongoDB:递增整数字段(C#)

[英]MongoDB: increment the integer field (C#)

How to increment integer field in atomic manner? 如何以原子方式增加整数字段? Can FindAndModify method helps? FindAndModify方法有帮助吗?

For example document contains field count and I wanna to increment it without retrieving full document and saving back. 例如,文档包含字段count ,我想在不检索完整文档和保存的情况下递增它。

I did leave a comment but I found it. 我确实发表了评论,但我找到了。 The $inc modifier will increment a field internally. $inc修饰符将在内部递增一个字段。 Completely atomically for this exact purpose. 完全原子地用于这个目的。

See here "Monog DB Atomic Operations" 在这里看到“Monog DB Atomic Operations”

Based on the answer from Paystey here's some code using version 2.1.0 of the C# driver, just in case someone else needs it: 根据Paystey的回答,这里有一些使用C#驱动版本2.1.0的代码,以防其他人需要它:

var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
var update = new BsonDocument("$inc", new BsonDocument { { "votes", 1 } });
var coll = db.GetCollection<BsonDocument>("collection");
var doc = coll.FindOneAndUpdateAsync(filter, update).Result;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM