【问题标题】:mongo push to array in aggregationmongo 在聚合中推送到数组
【发布时间】:2021-10-17 18:40:04
【问题描述】:

我有以下代码:

  const newArray = [];
  companies.items.forEach(async (item) => {
    if (item.parentCompanyID) {
      newArray.push({
        updateOne: {
          filter: { id: item?.parentCompanyID },
          update: [
            {
              $push: {
                branches: {
                  id: item?.id,
                  name: item.companyName,
                  parentId: item?.parentCompanyID,
                  type: item.companyType,
                  active: item.isActive,
                  number: item.companyNumber,
                  newlyAdded: { $eq: [{ $type: '$newlyAdded' }, 'missing'] },
                },
              },
            },
          ],
          upsert: true,
        },
      });
    } else {
      newArray.push({
        updateOne: {
          filter: { id: item?.id },
          update: [
            {
              $set: {
                id: item?.id,
                name: item.companyName,
                parentId: item?.parentCompanyID,
                type: item.companyType,
                active: item.isActive,
                number: item.companyNumber,
                newlyAdded: { $eq: [{ $type: '$newlyAdded' }, 'missing'] },
              },
            },
          ],
          upsert: true,
        },
      });
    }
  });
  await Company.bulkWrite(newArray);

这将通过 company.items 并且每个都将 updateOne 添加到 newArray 中,这将转到 bulkWrite。

我的问题在于$push,因为它需要在聚合管道中,当我在update 周围添加括号时,它将与MongooseError: Invalid update pipeline operator: "$push" 中断

我确信脚本可以简化,但我对 mongoDB 来说还是相当新的。如果项目没有得到 parentCompanyID,我需要将其插入公司,如果它确实有而不是推送到具有 parentCompanyID id 的相关公司的分支数组。

来自 company.items 数组的样本数据:

{
id: 5,
name: "Sports"
parentCompanyID: null
},
{
id: 51,
name: "Football"
parentCompanyID: 5
}

公司的 MongoDB 应该是这样的:

{
id: 5,
name: "Sports",
parentCompanyID: null,
branches: [{
   id: 51,
   name: "Football",
   parentCompanyID: 5
}]
}

希望这是有道理的。任何帮助,将不胜感激。我找不到任何类似的问题,我遇到的唯一一个问题是使用$concatArrays,但这也不起作用。

谢谢

编辑: 根据@Takis_ 回答这个现在有点工作。唯一的问题是当 $concatArrays 执行它时,它没有像$push 预期的那样推入数组。这是目前的结果,分支是一个具有嵌套数组的数组。如果有更多分支,它遵循相同的模式,最终可能会出现许多嵌套数组而不是 1 个对象数组。有什么想法吗?

{
  "id": 29683585,
  "name": "123",
  "parentId": null,
  "newlyAdded": true,
  "branches": [
    [
      null,
      {
        "id": 29693873,
        "name": "245",
        "parentId": 29683585
      }
    ],
    {
      "id": 29695646,
      "name": "789",
      "parentId": 29683585
    }
  ]
}

【问题讨论】:

  • $push 聚合运算符在 $group 中工作,但 $concatArrays 可以做同样的事情,如果你这样做 {$concatArrays : [ "$myarray" , [new-member] ]} (使其首先成为要添加的成员)但是对于大数组来说它很慢,例如,如果您在 $reduce 中喜欢其中的 1000 个,但在这里您只喜欢 1 个,那么就可以了。

标签: mongodb mongoose mongodb-query aggregation-framework


【解决方案1】:

现在已排序。感谢Takis 和他对$concatArrays 的指点,我能够做到这一点。

工作代码如下:

 newArray.push({
        updateOne: {
          filter: { id: item?.parentCompanyID },
          update: [
            {
              $set: {
                branches: {
                  $concatArrays: [
                    { $ifNull: ['$branches', []] },
                    [
                      {
                        id: item?.id,
                        name: item.companyName,
                        parentId: item?.parentCompanyID,
                        type: item.companyType,
                        active: item.isActive,
                        number: item.companyNumber,
                        newlyAdded: { $ne: ['$newlyAdded', null] },
                      },
                    ],
                  ],
                },
              },
            },
          ],
          upsert: true,
        },
      });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-14
    • 2018-04-29
    • 2013-07-19
    • 2020-05-09
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多