Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

326
Views
Altering immutable field _id error Mongo DB .net

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.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!