I keep getting this error:
System.AggregateException: One or more errors occurred. (Command findAndModify failed: After applying the update, the (immutable) field ‘_id’ was found to have been altered to _id: BinData(3, 00000000000000000000000000000000).) —> MongoDB.Driver.MongoCommandException: Command findAndModify failed: After applying the update, the (immutable) field ‘_id’ was found to have been altered to _id: BinData(3, 00000000000000000000000000000000).
I'm not trying to change the _id I'm just filtering on id. The record/document I update the old document with don't have Id I just want to update all the other fields/keys.
The code:
public T UpsertRecord<T>(string table, Guid id, T record)
{
var collection = db.GetCollection<T>(table);
var filter = Builders<T>.Filter.Eq("_id", id);
Task task = collection.FindOneAndReplaceAsync<T>(filter, record, new FindOneAndReplaceOptions<T, T> { IsUpsert = true });
task.Wait();
return record;
}
Below is the data im trying to post/update()responsePerson.Id:
//Update data
const string testNameUpdate = "MrSnail";
const int testTotalUpdate = 2;
var testPersonUpdate = new TestObj { name = testNameUpdate, total = testTotalUpdate };
var responseUpdate = _mongoDBFeedClient.UpsertRecord(collectionName, responsePerson.Id, testPersonUpdate);
I have tried replaceone method and such.
I’m using Atlas Cloud Mongo DB and its “sharded”. I don’t know if it have anything to do with this but it might. I don’t wanna change the id just update all the rest of the values id the document exists if it doesn´t I wanna create it.