我想根据同一文档中可用的slicingIndex键对文档中的数组进行切片,并推送到数组中.

[
  {
    slicingIndex: [0, 4, 7, 10],
    array: [
      { name: 1 },
      { name: 2 },
      { name: 3 },
      { name: 4 },
      { name: 5 },
      { name: 6 },
      { name: 7 },
      { name: 8 },
      { name: 9 },
      { name: 10 }
    ]
  }
]

帖子主题:Re:Колибри

{
  "array": [
    [
      { "name": 1 },
      { "name": 2 },
      { "name": 3 },
      { "name": 4 }
    ],
    [
      { "name": 5 },
      { "name": 6 },
      { "name": 7 }
    ],
    [
      { "name": 8 },
      { "name": 9 },
      { "name": 10 }
    ]
  ]
}

这是我在下面try 的聚合,但没有达到预期的效果:

db.collection.aggregate([
  {
    "$project": {
      slicingIndex: 1,
      array: {
        "$map": {
          "input": "$slicingIndex",
          "as": "num",
          "in": {
            "$slice": [
              "$array",
              "$$num",
              {
                "$subtract": [
                  {
                    "$arrayElemAt": ["$slicingIndex", { "$indexOfArray": ["$slicingIndex", "$$num"] }]
                  },
                  "$$num"
                ]
              }
            ]
          }
        }
      }
    }
  }
])

推荐答案

MongoDb Playground Link:https://mongoplayground.net/p/oNzBVLRfIU0

$add & $indexOfArray used to take one step ahead element from current pointer in array like (n+1).
$let : I have used it as optional to make variable reusable if needed.
$map : To traverse array elements.
$arrayElemAt : To get from element from array.
$subtract : To perform operation like [ (n+1) - n ] where n is index in array.
$filter : To filter out elements (in our case: null).

db.collection.aggregate([
  {
    "$project": {
      array: {
        "$map": {
          "input": "$slicingIndex",
          "as": "elem",
          "in": {
            "$let": {
              "vars": {
                //create variable that holds element one index+1 to the current index
                "elemOneStepHeadToElem": {
                  "$arrayElemAt": [
                    "$slicingIndex",
                    {
                      $add: [
                        1,
                        {
                          "$indexOfArray": [
                            "$slicingIndex",
                            "$$elem"
                          ]
                        }
                      ]
                    }
                  ]
                }
              },
              "in": {
                "$slice": [
                  "$array",
                  "$$elem",
                  {
                    "$subtract": [
                      "$$elemOneStepHeadToElem",
                      "$$elem"
                    ]
                  }
                ]
              }
            }
          }
        }
      }
    }
  }//remove null from array from last index
  ,
  {
    "$project": {
      array: {
        "$filter": {
          "input": "$array",
          "cond": {
            $ne: [
              "$$this",
              null
            ]
          }
        }
      }
    }
  }
])

Mongodb相关问答推荐

MongoDB 4.4如何使用未知密钥更新dict

无法在Ubuntu 22.04上安装MongoDB 7.0

如何在MongoSH中的现有文档中插入字段

MongoDB 按 created_at 月聚合

mongoDB文档数组字段中的唯一项如何

从嵌套数组中 id 匹配的另一个集合中获取所有字段

MongoDB 存储大量指标/分析数据的方法

TypeError:Cannot read property '_id' of undefined

在 MongoDB 中打开连接的 SocketTimeout

使用 AngularJs 和 MongoDB/Mongoose

MongoDB 的 Java 语法

SELECT 字段 AS `anothername` 的 mongodb 等效项

MongoDB 是否重用已删除的空间?

从命令行创建 MongoDB 用户

使用已排序的数据获取不同的值

REACT 获取发布请求

如何使用node.js http服务器从mongodb返回大量行?

如何为 node.js 中的 MongoDB 索引指定 javascript 对象中的属性顺序?

Mongo:匹配聚合查询中的日期似乎被忽略了

MongoDB Compass 中 JSON 输入意外结束