简体   繁体   English

mongodb C#驱动程序在使用Update.Push时不在嵌入的对象列表中生成新添加文档的ObjectId

[英]mongodb C# driver not generating ObjectId of newly added document in embedded list of objects when using Update.Push

I am using mongodb C# driver and trying to add "Goal" Entity in the "List", contained in Client class, but mongodb always returns an empty objectId og Goal. 我正在使用mongodb C#驱动程序并尝试在Client列中包含的“List”中添加“Goal”实体,但是mongodb总是返回一个空的objectId og目标。

Client.cs Client.cs

    public class Client
    {
    public string UserName { get; set; }

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

    [BsonIgnore]
    public string ClientIdString
    {
        get
        {
            return this.ClientId.ToString();
        }
    }

    [BsonRequired]
    public string ClientName { get; set; }

    public string EmailAddress { get; set; }

    public string CompanyName { get; set; }

    public List<Goal> Goals { get; set; }
    }

Goal.cs Goal.cs

public class Goal
{

    public ObjectId GoalId { get; set; }

    [BsonRequired]
    public string Title { get; set; }

    public string Description { get; set; }

    [BsonDefaultValue(State.NotStarted)]
    public State GoalState { get; set; }

    [BsonIgnore]
    public string StateString 
    {
        get
        {
            return this.GoalState.ToString();
        }
    }

}

and i am trying to add Goal to client with following code: 我正在尝试使用以下代码将目标添加到客户端:

IMongoQuery query = Query<Client>.EQ(eq => eq.ClientId, id);
                IMongoUpdate update = Update<Client>.Push<Goal>(push => push.Goals, goal);

                WriteConcernResult result = this.ClientCollection.Update(query, update);

But every time mongodb returns an empty objectId of newly added Goal in the List in client. 但每次mongodb返回客户端列​​表中新添加的目标的空objectId。

Kindly guide me what i am doing wrong? 请指导我做错了什么?

Thanks in advance. 提前致谢。 :) :)

Kindly guide me what i am doing wrong? 请指导我做错了什么?

Nothing really, but you're expecting the wrong behavior. 没什么,但你期待错误的行为。

Embedded documents don't have an _id . 嵌入的文档没有_id Of course, you can force mongodb to create a field that happens to have that name, but unlike the root _id , such a field has no special semantics and there's no default index. 当然,你可以强制mongodb创建一个恰好具有该名称的字段,但与root _id不同,这样的字段没有特殊的语义,并且没有默认索引。 The lack of these special semantics is also the reason why the driver doesn't bother generating a value for the field for you. 缺少这些特殊语义也是驱动程序无需为您生成字段值的原因。

Generally speaking, having a unique index in an embedded document is often a sign of a malformed data structure. 一般而言,在嵌入式文档中具有唯一索引通常是格式错误的数据结构的标志。 Make sure such an id is strictly required. 确保严格要求这样的id。 If the id must be globally unique, it appears the embedded document might also make sense or be relevant if it had another parent, which indicates that it should be a first-level citizen, ie have a collection of its own. 如果id必须是全局唯一的,那么嵌入式文档似乎也有意义或相关如果它有另一个父级,这表明它应该是第一级公民,即拥有自己的集合。

Remember that this also a question of ownership - what happens with concurrent writes? 请记住,这也是一个所有权问题 - 并发写入会发生什么? Of course, you can go to great lengths and only use atomic modifiers to allow different writes to one document, but that is unnecessarily complicated IMHO. 当然,你可以不遗余力地只使用原子修饰符来允许对一个文档进行不同的写入,但这是不必要的复杂恕我直言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM