Reputation: 9752
I'm trying to update multiple fields in a document using MongoDb.Driver
2.4.4.
I have searched Stackoverflow and all the potential questions that are similar are for older versions which allow chaining of .Set()
functions. It does not appear 2.4.4 allows this.
My code currently comprises of
var update = MongoDB.Driver.Builders<UserLocation>.Update.Set(x => x.Loction, coordinates);
var updateResult = await this._mongo.Taps.UpdateOneAsync(filter, update);
I would like to update an additional field here as well and have them all update in one transaction.
Upvotes: 0
Views: 2520
Reputation: 31282
I have searched Stackoverflow and all the potential questions that are similar are for older versions which allow chaining of .Set() functions. It does not appear 2.4.4 allows this.
Nothing has changed here, MongoDb.Driver of 2.4.4 version still allows chaining Set
calls.
UpdateDefinitionBuilder<TDocument>.Set()
returns instance of UpdateDefinition<TDocument>
. Chained Set()
calls are done through extension method for UpdateDefinition<TDocument>
class. This extension method is defined in UpdateDefinitionExtensions
class from MongoDB.Driver
namespace.
I bet you don't have using directive for MongoDB.Driver
namespace, because in your code snippet you specify MongoDB.Driver
namespace explicitly. That's why compiler does not know about Set()
extension method that should be called.
To fix the problem and have chained Set()
calls compiled, just add
using MongoDB.Driver;
at the top of your source file. Then you'll be able to chain Set()
calls:
var update = Builders<UserLocation>.Update.Set(x => x.Loction, coordinates)
.Set(x => x.SomeField1, someValue1)
.Set(x => x.SomeField2, someValue2);
var updateResult = await this._mongo.Taps.UpdateOneAsync(filter, update);
Upvotes: 4