MongoDb C# 驱动程序检索增量更新的值
MongoDb C# Driver retrieve Value of increment update
我使用以下 inc 语句进行更新以增加字段的值
var update = Updates.Inc(x => x.Version, 1);
await collection.FindAndUpdateOneAsync(myQuery,update);
我想从版本 中检索新(或旧)值。有内置的方法吗?
出于交易考虑,我不想进行新的单独查询。
您可以尝试 findOneAndUpdate
和 projection
选项到 return 文档。
至return旧值
db.collection.findOneAndUpdate(
{},
{ $inc: { Version: 1 } },
{ projection: { Version : 1 }}
)
要return更新值,设置returnNewDocument
标志为true
db.collection.findOneAndUpdate(
{},
{ $inc: { Version: 1 } },
{ projection: { Version : 1 }, returnNewDocument : true }
)
这里有更多
https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/
这里是如何做 Veeram 所说的,但在 c# 中。
var update = new UpdateDefinitionBuilder<Widget>().Inc(n => n.Version, 1);
var options = new FindOneAndUpdateOptions<Widget>();
options.ReturnDocument = ReturnDocument.After;
options.Projection = new ProjectionDefinitionBuilder<Widget>().Include(n => n.Version);
var result = col.FindOneAndUpdate<Widget>(n => true, update, options );
TL&DR:
使用投影到 return 插入后的值:
var opts = new FindOneAndUpdateOptions<BsonDocument>()
{
ReturnDocument = ReturnDocument.After
};
var result = await collection.FindOneAndUpdateAsync(filter, update, opts);
解释:
当您使用 FindOneAndUpdateAsync() 函数时,MongoDB 默认情况下不会 return 插入后的完整更新值。为了获得价值,您必须利用投影,它控制函数的 'output'(用外行术语)。
完整用例
假设car_name是一个字符串,在上面的函数中提供。这会找到一个特定的汽车名称,然后分别更新其 country_code 和 country_name。
IMongoCollection<BsonDocument> collection = this._database.GetCollection<BsonDocument>("cars");
var filter = new BsonDocument("cars", car_name);
var opts = new FindOneAndUpdateOptions<BsonDocument>()
{
ReturnDocument = ReturnDocument.After
};
var update = Builders<BsonDocument>.Update.Set("country_name",country_name).Set("country_code",country_code);
var result = await collection.FindOneAndUpdateAsync(filter, update, opts);
return result;
一个simpler/another方式:
var filter = Builders<MyDocument>.Filter.Eq("myId", id)
var update = new UpdateDefinitionBuilder<MyDocument>().Inc(p => p.MyProperty, 1);
await myCollection.UpdateOneAsync(filter, update);
我使用以下 inc 语句进行更新以增加字段的值
var update = Updates.Inc(x => x.Version, 1);
await collection.FindAndUpdateOneAsync(myQuery,update);
我想从版本 中检索新(或旧)值。有内置的方法吗?
出于交易考虑,我不想进行新的单独查询。
您可以尝试 findOneAndUpdate
和 projection
选项到 return 文档。
至return旧值
db.collection.findOneAndUpdate(
{},
{ $inc: { Version: 1 } },
{ projection: { Version : 1 }}
)
要return更新值,设置returnNewDocument
标志为true
db.collection.findOneAndUpdate(
{},
{ $inc: { Version: 1 } },
{ projection: { Version : 1 }, returnNewDocument : true }
)
这里有更多 https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/
这里是如何做 Veeram 所说的,但在 c# 中。
var update = new UpdateDefinitionBuilder<Widget>().Inc(n => n.Version, 1);
var options = new FindOneAndUpdateOptions<Widget>();
options.ReturnDocument = ReturnDocument.After;
options.Projection = new ProjectionDefinitionBuilder<Widget>().Include(n => n.Version);
var result = col.FindOneAndUpdate<Widget>(n => true, update, options );
TL&DR:
使用投影到 return 插入后的值:
var opts = new FindOneAndUpdateOptions<BsonDocument>()
{
ReturnDocument = ReturnDocument.After
};
var result = await collection.FindOneAndUpdateAsync(filter, update, opts);
解释:
当您使用 FindOneAndUpdateAsync() 函数时,MongoDB 默认情况下不会 return 插入后的完整更新值。为了获得价值,您必须利用投影,它控制函数的 'output'(用外行术语)。
完整用例
假设car_name是一个字符串,在上面的函数中提供。这会找到一个特定的汽车名称,然后分别更新其 country_code 和 country_name。
IMongoCollection<BsonDocument> collection = this._database.GetCollection<BsonDocument>("cars");
var filter = new BsonDocument("cars", car_name);
var opts = new FindOneAndUpdateOptions<BsonDocument>()
{
ReturnDocument = ReturnDocument.After
};
var update = Builders<BsonDocument>.Update.Set("country_name",country_name).Set("country_code",country_code);
var result = await collection.FindOneAndUpdateAsync(filter, update, opts);
return result;
一个simpler/another方式:
var filter = Builders<MyDocument>.Filter.Eq("myId", id)
var update = new UpdateDefinitionBuilder<MyDocument>().Inc(p => p.MyProperty, 1);
await myCollection.UpdateOneAsync(filter, update);