如何更新使用C#驱动程序在MongoDB中包含在数组中的子文档在数组中的子文档的字段? [英] How to update a field in an array's subdocument contained in an array's subdocument in MongoDB using C# driver?

查看:132
本文介绍了如何更新使用C#驱动程序在MongoDB中包含在数组中的子文档在数组中的子文档的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有像这样的结构在MongoDB中的文档集合(有多个容器文件,每个都包含会话,每个会话包含1个订单,每个订单包含多个项):

I have a collection of documents in MongoDB with a structure like this (there are multiple container documents, each containing Sessions, each session contains 1 Order, each Order contains multiple Items):

{
  "Sessions" : [{
      "ID" : "1882a092-d395-45d1-b36b-e2fa3df81b95",
      "Order" : {
        "Items" : [{
            "_id" : "a9c94a5e-10ef-433d-9c63-f4555eb7c2a7",
            "Title" : "UPDATE THIS",
          }]
      }
    }],
  "_id" : "1b640bc4-fdb4-49b1-9c60-e4c6f1bd0405"
}

我想更新标题给定项目在会话订单内,而明知 _id 的项目(我也知道的ID会议是否有帮助):

I would like to update Title of a given Item within an Order in a Session while knowing the _id of the Item (I also know ID of the Session if that helps):

我曾尝试以下内容:

col.Update(Query.EQ("Sessions.ID", sessionID),
                Update.Set("Sessions.$.Order.Items.Title", newTitle));

col.Update(Query.EQ("Sessions.Order.Items._id", id),
                Update.Set("Sessions.Order.Items.$.Title", newTitle));



这两个抛出一个异常 WriteConcern检测到错误'不能使用的部分(会话Sessions.Order.Items.0.Status的)遍历元素... 。我使用 Query.And 这可能让那么一点感觉他们是不值得张贴在这里也合并审理查询的不同组合。

Both throw an exception WriteConcern detected an error 'cannot use the part (Sessions of Sessions.Order.Items.0.Status) to traverse the element .... I also tried different combinations of combined queries using Query.And which probably make so little sense that they are not worth posting here.

如何在包含在使用C#的驱动程序文件数组的子文档更新场?

我知道这不能做单独的位置运算符(与位置运营商不会神奇匹配内部数组)。

I realize this cannot be done with the positional operator alone (and the positional operator will not magically match inner array).

我要寻找的开箱解决方案如何完成这种更新不改变架构。

I am looking for out of the box solutions how to accomplish this kind of update without changing the schema.

推荐答案

这@Disposer回答帮我找到了一个解决方案。它传播的多个领域,我的代码库,但我想与他人分享的基本精神谁可能有类似的问题。

Answer from @Disposer helped me find a solution for this. It spreads multiple areas in my codebase but I would like to share the basic gist with others who may have a similar problem.

这些步骤如下:


  • 锁定给定的顺序,不要让物品添加进去(在应用程序级别)。

  • 另外,使用一个额外的 WriteCount 字段的组合,并使用 findAndModify 以原子更新文档仅当它尚未在此期间被更新(更新,如果当前)。

  • Lock the given Order and do not allow items to be added into it (on application level).
  • Alternatively, use a combination of an additional WriteCount field and by using findAndModify to atomically update the document only if it has not been updated in the meantime ("update if current").

然后,当更新标题对于给定的项目:

Then, when updating Title for a given item:


  • 使用存储 WriteCount

  • 确定项指数

  • 使用相结合的项目索引与位置运算符: $ 来标识会话,一个整数首页为目标的项目,同时聘请更新,如果目前的战略使用 WriteCount 字段

  • use stored WriteCount
  • determine item index
  • use the item index in combination with the positional operator: $ to identify the session, an integer index to target the item, while employing "update if current" strategy using the WriteCount field.

然后将更新的部分就变成了:

The update part then becomes:

"Sessions.$.Order.Items." + index + ".Title"

这篇关于如何更新使用C#驱动程序在MongoDB中包含在数组中的子文档在数组中的子文档的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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