mongodb upsert / addToSet / embedded Documents

时间:2016-03-21 01:17:59

标签: mongodb meteor mongodb-query

我的文档看起来像这样,一个id最多有20个workCenters,每个workCenter都有一些问题。一旦id前进到下一个工作中心,每个workCenter阵列都会关闭或阻止以进一步输入。

{
  _id: "C7700258wcL4MSB050",
  WorkCenters: [
    {
      workCenter: "L4MSB050",
      Issues: [
        {
          issueType: "Material Movement",
          issueDuration: 14
        },
        {
          issueType: "Communication COA",
          issueDuration: 20
        },
        {
          issueType: "Lack of Tool",
          issueDuration: 12
        }
      ]
    },
    {
      workCenter: "L4MSB060",
      Issues: [
        {
          issueType: "Material Movement",
          issueDuration: 14
        },
        {
          issueType: "Communication COA",
          issueDuration: 20
        },
        {
          issueType: "Lack of Tool",
          issueDuration: 12
        }
      ]
    }
  ]
}

到目前为止我所拥有的是:

TaktIssue.update({_id: centerId}, {$addToSet: {WorkCenters: {workCenter: workCenter, Issues: {issueType: issueType, issueDuration: minutes_2}}}});

这就是我想要的......

{
  _id: "C7700254workCenter",
  WorkCenters: [
    {
      workCenter: "L4MSB010",
      Issues: {
        issueType: "Tech Error",
        issueDuration: 0
      }
    }
  ]
}

但是同一个workCenter的第二个条目是第一个条目的完整添加,而不仅仅是issueType和时间。 并且新的workCenter不会添加新阵列。

   {
  _id: "C7700254workCenter",
  WorkCenters: [
    {
      workCenter: "L4MSB010",
      Issues: {
        issueType: "Tech Error",
        issueDuration: 0
      }
    },
    {
      workCenter: "L4MSB010",
      Issues: {
        issueType: "Lack of Tool",
        issueDuration: 0
      }
    },
    {
      workCenter: "L4MSB020",
      Issues: {
        issueType: "Material Movement",
        issueDuration: 0
      }
    },
    {
      workCenter: "L4MSB020",
      Issues: {
        issueType: "Tech Error",
        issueDuration: 0
      }
    }
  ]
}

我希望它清楚地说明了我想要完成的事情。 任何帮助或提示都非常感谢。 感谢

1 个答案:

答案 0 :(得分:0)

workcenters文档为空时,如下所示

> db.workcenters.find()
{ "_id" : ObjectId("56ef4f51fc6035db1abbf8ce"), "WorkCenters" : [ ] }

您可以使用workcenter插入新的$addToSet$ne,如下所示

> db.workcenters.update({_id: ObjectId('56ef4f51fc6035db1abbf8ce'),
                        'WorkCenters.workCenter': {$ne: 'L4MSB010'}},
                       {$addToSet: {
                             WorkCenters: {
                                  workCenter: 'L4MSB010', 
                                  Issues: [{issueType: 'Tech Error', 
                                            issueDuration: 0}]}}})

结果是

> db.workcenters.find()
{ "_id" : ObjectId("56ef4f51fc6035db1abbf8ce"), "WorkCenters" : [ { "workCenter" : "L4MSB010", "Issues" : [ { "issueType" : "Tech Error", "issueDuration" : 0 } ] } ] }

然后尝试将新的workcenter添加到现有的workcenter $eq,如下所示

> db.workcenters.update({_id: ObjectId('56ef4f51fc6035db1abbf8ce'), 
                        'WorkCenters.workCenter': {$eq: 'L4MSB010'}},
                        {$addToSet: {
                             'WorkCenters.$.Issues': {
                                     issueType: 'Communication COA', 
                                     issueDuration: 0}}}) 

结果:

> db.workcenters.find()
{ "_id" : ObjectId("56ef4f51fc6035db1abbf8ce"), "WorkCenters" : [ { "workCenter" : "L4MSB010", "Issues" : [ { "issueType" : "Tech Error", "issueDuration" : 0 }, { "issueType" : "Communication COA", "issueDuration" : 0 } ] } ] }