【问题标题】:Push items into array of objects with array attribute in mongoose在猫鼬中将项目推送到具有数组属性的对象数组中
【发布时间】:2021-08-08 13:56:25
【问题描述】:

您好,我正在尝试将一个元素添加到一个对象内的数组中,而该对象又在一个数组内,下面是结构。

// Schema called "Team" with mongoose
category: [
{
  seasson: { type: String, required: true },
  categories: [{ type: String, required: true }]
}]
// In code looks like:
[
  {
    seasson: "The seasson name 1",
    categories: ["categoryOne", "categoryTwo"]
  }
  {
    seasson: "The seasson name 2",
    categories: ["categoryOne"] // I want to make push in this array the value "categoryTwo"
  },
]
// I´m trying something like following code:
const status = await Team.updateOne(
  {
    _id: mongoose.Types.ObjectId(teamId),
  },
  { $addToSet: { "category.$last.categories": "categoryTwo"} }
)

当一个数组必须被推入对象时,它将位于主数组的最后一个位置。老实说,我一直在努力寻找一种方法,但我想不出任何可行的方法。 提前致谢。

【问题讨论】:

  • category 您的架构中的字段吗?
  • 是我架构中的字段之一

标签: node.js arrays mongodb mongoose


【解决方案1】:

没有任何标识更新数组的最后一个元素没有直接的方法,从MongoDB 4.2开始可以使用update with aggregation pipeline

  • $map 迭代 category 数组的循环
  • $mergeObjects 将当前 category 对象与更新后的 categories 字段合并
  • $lastcategory.seasson 获取最后一个元素值
  • $cond 检查条件,如果高于最后一个元素的值和当前对象 session 匹配,则执行更新操作,否则返回现有值
  • $setUnion 连接 categories 的新值,如果存在,则替换
let category = "categoryTwo";
const status = await Team.updateOne(
  { _id: mongoose.Types.ObjectId(teamId) },
  [{
    $set: {
      category: {
        $map: {
          input: "$category",
          in: {
            $mergeObjects: [
              "$$this",
              {
                categories: {
                  $cond: [
                    {
                      $eq: [
                        { $last: "$category.seasson" },
                        "$$this.seasson"
                      ]
                    },
                    { $setUnion: ["$$this.categories", [category]] },
                    "$$this.categories"
                  ]
                }
              }
            ]
          }
        }
      }
    }
  }]
)

Playground

【讨论】:

    【解决方案2】:

    下面的查询,在数组的最后一个成员的类别中添加“categoryTwo” 类别。我想这就是你想要的。

    如果您下次可以以初始形式提供文档,描述您想要的查询,并以有效 JSON 形式提供最终形式的文档,以便人们可以更轻松地帮助您。

    你可以try the code here 它的管道更新,需要 MongoDB >= 4.2

    数据输入(集合)

    [
      {
        "_id": 1,
        "category": [
          {
            "seasson": "The seasson name 1",
            "categories": [
              "categoryOne",
              "categoryTwo"
            ]
          },
          {
            "seasson": "The seasson name 2",
            "categories": [
              "categoryOne"
            ]
          },
          
        ]
      }
    ]
    

    查询

    db.collection.update({
      "_id": {
        "$eq": 1
      }
    },
    [
      {
        "$addFields": {
          "category": {
            "$let": {
              "vars": {
                "without_last": {
                  "$slice": [
                    "$category",
                    0,
                    {
                      "$subtract": [
                        {
                          "$size": "$category"
                        },
                        1
                      ]
                    }
                  ]
                },
                "last_member": {
                  "$arrayElemAt": [
                    {
                      "$slice": [
                        "$category",
                        -1,
                        1
                      ]
                    },
                    0
                  ]
                }
              },
              "in": {
                "$concatArrays": [
                  "$$without_last",
                  [
                    {
                      "$mergeObjects": [
                        "$$last_member",
                        {
                          "categories": {
                            "$concatArrays": [
                              "$$last_member.categories",
                              [
                                "categoryTwo"
                              ]
                            ]
                          }
                        }
                      ]
                    }
                  ]
                ]
              }
            }
          }
        }
      }
    ])
    

    结果

    [
      {
        "_id": 1,
        "category": [
          {
            "categories": [
              "categoryOne",
              "categoryTwo"
            ],
            "seasson": "The seasson name 1"
          },
          {
            "categories": [
              "categoryOne",
              "categoryTwo"
            ],
            "seasson": "The seasson name 2"
          }
        ]
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 2019-03-22
      • 2021-02-09
      • 2019-10-13
      • 2018-09-19
      • 2016-02-28
      相关资源
      最近更新 更多