Facing this weird issue in ASP.Net Core 3.0

I started learning ASP.Net Core Web API development a few days back. For the same, I am following the official tutorial document by Microsoft.

However, there is an issue. A major one I guess. Since I am also new in MongoDB, I found that when running a particular code from the tutorial
the code section:
public void Update(string id, Book bookIn) =>
_books.ReplaceOne(book => book.Id == id, bookIn);

returns an error:
MongoDB.Driver.MongoCommandException: Command findAndModify failed: After applying the update, the (immutable) field ‘_id’ was found to have been altered to _id: null.

Do note, the code on Microsoft Site is exactly the same in my project.

Hi @Kunal_Das, welcome to the forum!

Based on the code and the error message, it suggests that the bookIn variable contains _id field with a null value. Try to omit the Id field of the class, see also db.collection.findAndModify for more information.

If you have further question, it would be helpful to provide:

  • MongoDB server version
  • MongoDB .NET driver version
  • Relevant code snippets
  • Relevant parameters/input data to reproduce

I’d recommend to enrol in a free online course on MongoDB University M220N: MongoDB for .NET Developers to learn about .NET and MongoDB.


1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.