Upserting在蒙戈DB和标识问题 [英] Upserting in Mongo DB and the Id problem

查看:180
本文介绍了Upserting在蒙戈DB和标识问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须同时upserting到蒙戈DB使用官方C#驱动程序有问题。

 公共抽象类AggregateRoot 
{
///<总结>
///所有的MongoDB文档必须有一个ID,我们在这里声明,
///< /总结>
保护AggregateRoot()
{
n = ObjectId.GenerateNewId();
}

[BsonId]
公众的ObjectId标识{搞定;组; }
}



我的实体已经有了ID-S,但我不得不创建蒙戈特定ID为它工作,作为一个集合中的所有文件应该有一个。那么现在我收到我的系统的新实体将产生一个新的蒙戈Id和我得到的蒙戈不能更改文档老异常_id。有一些变通?




让我描述设计了一下。这将是存储为文件
的所有实体从AggregateRoot其中有其中的
ID生成继承。每个子文档自动拥有其ID生成
和我没有问题,与此有关。在AggregateRoot
中的ID是引进
MongoCollection检索数据时列出来纠正问题,并引入了新一代所以ID-S
是不同的。现在,我们可以移动ID生成保存方法
,因为更新的新实体有一个新的ID生成。但是,
将意味着对团队的每个开发绝不能忘记宝库,它是有风险的生成ID-S
。这将是更好的只是忽略ID
比蒙戈映射,如果有可能,而不是在所有



具有
AggregateRoot类< DIV CLASS =h2_lin>解决方案

您似乎明确地设置编号为插入和更新的价值。这很好的插入,所有新对象需要一个 _id 价值,但是更新你不能更改 _id 在它的创建后,现有的文档。



尽量不要设置编号价值可言。如果没有指定在插入前值,驱动程序使用内置的 IdGenerator 类来生成一个新的_id值,因此,如果它是一个的ObjectId键入将使用 ObjectIdGenerator 。那么这两个你插入和更新做工精细。


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

public abstract class AggregateRoot
{
    /// <summary>
    /// All mongoDb documents must have an id, we specify it here
    /// </summary>
    protected AggregateRoot()
    {
        Id = ObjectId.GenerateNewId();
    }

    [BsonId]
    public ObjectId Id { get; set; }
}

My entities already have the id-s but I had to create the mongo specific Id for it to work, as all the documents in a collection should have one. Now then I receive a new entity in my system a new Mongo Id is generated and I get the mongo cannot change _id of a document old exception. Is there some work-around?

Let me describe the design a bit. All the entities which would be stored as documents were inheriting from AggregateRoot which had the id generation in it. Every sub-document had its id generated automatically and I had no problem with this. The id in AggregateRoot was introduced to correct the problem when retrieving data from MongoCollection to List and the generation was introduced so the id-s are different. Now we can move that id generation to save methods because the new entity for update had a new id generation. But it would mean that every dev on the team must not forget generating id-s in repository which is risky. It would be nicer just to ignore the id than mapping from mongo if it is possible and not to have AggregateRoot class at all

解决方案

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.

这篇关于Upserting在蒙戈DB和标识问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆