用管道(聚合或更新)以特定索引将项目推入数组-MongoDB

发布于 2025-02-13 18:42:46 字数 1001 浏览 0 评论 0 原文

,我寻找了一种常见的做法,可以将项目插入管道内的特定索引中的数组,然后找不到一个。 假设我的文档看起来像:

[
  {
    _id: ObjectId("62c2e94e65f32725f8f62b79"),
    updatedAt: ISODate("2022-06-29T13:10:36.659Z"),
    createdAt: ISODate("2022-06-29T08:06:51.264Z"),
    userID: 1,
    myImage: "imageC",
    images: [
      "imageA",
      "imageB",
      "imageD",
      "imageE",
      "imageF"
    ]
  }
]

我想将值插入字段 myimage images array,specificaly in Index 2中,所以预期的结果是一个更新的文档:

[
  {
    _id: ObjectId("62c2e94e65f32725f8f62b79"),
    updatedAt: ISODate("2022-06-29T13:10:36.659Z"),
    createdAt: ISODate("2022-06-29T08:06:51.264Z"),
    userID: 1,
    myImage: "imageC",
    images: [
      "imageA",
      "imageB",
      "imageC",
      "imageD",
      "imageE",
      "imageF"
    ]
  }
]

inspired by another question, I looked for a common practice for inserting an item into an array at a specific index inside a pipeline, and could not find one.
Assuming my document looks like:

[
  {
    _id: ObjectId("62c2e94e65f32725f8f62b79"),
    updatedAt: ISODate("2022-06-29T13:10:36.659Z"),
    createdAt: ISODate("2022-06-29T08:06:51.264Z"),
    userID: 1,
    myImage: "imageC",
    images: [
      "imageA",
      "imageB",
      "imageD",
      "imageE",
      "imageF"
    ]
  }
]

And I want to insert the value in field myImage to images array, specificaly at index 2, so the expected result is an updated document:

[
  {
    _id: ObjectId("62c2e94e65f32725f8f62b79"),
    updatedAt: ISODate("2022-06-29T13:10:36.659Z"),
    createdAt: ISODate("2022-06-29T08:06:51.264Z"),
    userID: 1,
    myImage: "imageC",
    images: [
      "imageA",
      "imageB",
      "imageC",
      "imageD",
      "imageE",
      "imageF"
    ]
  }
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怕倦 2025-02-20 18:42:46

这可以使用聚合管道或管道更新来完成。在这种情况下,简单的选项是更新管道。

这里的想法是在 $$ value $ size 中使用 $降低步骤以找到正确的位置将项目插入到:

db.collection.update(
{userID: 1},
[
  {$set: {
      images: {
        $reduce: {
          input: "$images",
          initialValue: [],
          in: {$concatArrays: [
              "$value",
              {$cond: [
                  {$eq: [{$size: "$value"}, index]},
                  ["$myImage", "$this"],
                  ["$this"]
                ]
              }
            ]
          }
        }
      }
    }
  }
])

查看其在

This can be done using an aggregation pipeline or an update with pipeline. In this case the simple option is an update pipeline.

The idea here is to use the $size of the $$value in the $reduce step to find the right place to insert the item into:

db.collection.update(
{userID: 1},
[
  {$set: {
      images: {
        $reduce: {
          input: "$images",
          initialValue: [],
          in: {$concatArrays: [
              "$value",
              {$cond: [
                  {$eq: [{$size: "$value"}, index]},
                  ["$myImage", "$this"],
                  ["$this"]
                ]
              }
            ]
          }
        }
      }
    }
  }
])

See how it works on the playground example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文