Upserting in Mongo DB and the Id problem

前端 未结 2 2108
情书的邮戳
情书的邮戳 2021-02-12 21:59

I have a problem while upserting to mongo db using the official C# driver.

public abstract class AggregateRoot
{
    /// 
    /// All mongoDb docu         


        
2条回答
  •  半阙折子戏
    2021-02-12 22:48

    Looks like you might be explicitly setting the Id value for both inserts and updates. That's fine for inserts, all new objects need an _id value, however for updates you're not allowed to change the value of _id on an existing document after it's created.

    Try not setting the Id value at all. If you don't specify a value before inserting, the driver uses built-in IdGenerator classes to generate a new _id value, so if it's an ObjectId type it'll use the ObjectIdGenerator. Then both your inserts and updates work fine.

提交回复
热议问题