添加“索引"每个数组元素到元素本身的关系 [英] Add the "index" of each array element to the element itself

查看:103
本文介绍了添加“索引"每个数组元素到元素本身的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用聚合框架,我想将数组内每个元素的索引添加到子文档本身.

Using the aggregation framework I want to add the index of each element inside the array to the subdocument itself.

在稍后的阶段中,我想$unwind元素,但是然后我需要在myResults数组内部的旧索引.

In a later stage I'd like to $unwind the elements, but then I need the old index inside the myResults Array.

当前结构:

{ myResults : [
    { foo: "bar"
    },
    { answer: 42
    }
]

目标结构:

{ myResults : [
    { index: 0,  // <-- this should be added
      foo: "bar"
    },
    { index: 1,  // <-- this should be added
      answer: 42
    }
]

注意:我不想更新元素,我只想在聚合管道的后期使用索引.

Note: I don't want to update the elements, - I just want to use the index in a later stage of the aggregation pipeline.

推荐答案

您将希望在 MongoDB Jira中对该问题进行投票系统:

$unwind中添加选项以发出数组索引

Add option to $unwind to emit array index

它提供了您所需要的确切功能.目前尚未计划将其用于特定的将来版本,但是通常受所需数量的影响.向其添加注释以解释您的用例并对其进行投票将影响相对于

It provides exact functionality that you are asking for. It is not currently scheduled for a particular future release, but that usually is influenced by how much it is needed. Adding a note to it explaining your use case and voting on it will influence its prioritization relative to other unscheduled aggregation framework tickets.

这篇关于添加“索引"每个数组元素到元素本身的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆