云数据库查询数据的问题,数组下标值来自另一个字段的内容,如何写?
发布于 6 年前 作者 zhe 12212 次浏览 来自 官方Issues

集合数据如下:

[

    {_id: 1, index: 1, list: [“a”, “b”]},

    {_id: 1, index: 0, list: [“c”, “d”]}

]

我想最终得到的数据如下:

[

    {_id: 1, name: “b”},

    {_id: 1, name: “c”}

]

请问怎么实现?

也就是list数组的下标来自index的内容,这个应该怎么写?

1 回复

var $ = db.command.aggregate

db.collection(‘pagecache’)

.aggregate()

.project({

    name: $.arrayElemAt([’$list, ‘$index’])

})

.end().then(res=>{

    console.log(res.list)

})

回到顶部