I'm adding an UpdateCustomer method that passes in the modified customer to be persisted to the DB. But I've come across an error when calling ReplaceOneAsync on the updated document.
I've consulted the following example and api reference, which both state to pass ReplaceOneAsync a filter and document parameters.
But the specific errors are thrown because of incorrect parameters, as stated below:
Error 1 The best overloaded method match for 'MongoDB.Driver.IMongoCollection<MongoDBApp.Models.CustomerModel>.ReplaceOneAsync(MongoDB.Driver.FilterDefinition<MongoDBApp.Models.CustomerModel>, MongoDBApp.Models.CustomerModel, MongoDB.Driver.UpdateOptions, System.Threading.CancellationToken)' has some invalid arguments
Error 2 Argument 2: cannot convert from 'MongoDB.Bson.BsonDocument' to 'MongoDBApp.Models.CustomerModel'
Anyone have any hints on making sense of the error?
The UpdateCustomer method:
public async Task UpdateCustomer(CustomerModel customer)
{
var collection = StartConnection();
var filter = Builders<CustomerModel>.Filter.Where(x => x.Id == customer.Id);
BsonDocument doc = new BsonDocument();
doc["_id"] = customer.Id;
doc["firstName"] = customer.FirstName;
doc["lastName"] = customer.LastName;
doc["email"] = customer.Email;
//error thrown here on the ReplaceOneAsync params..
await collection.ReplaceOneAsync(filter, doc);
}
And the associated StartConnection method:
private static IMongoCollection<CustomerModel> StartConnection()
{
var client = new MongoClient(connectionString);
var database = client.GetDatabase("orders");
//Get a handle on the customers collection:
var collection = database.GetCollection<CustomerModel>("customers");
return collection;
}