$arrayElemAt (aggregation)

    在本页面

    $arrayElemAt

    3.2版中的新功能。

    返回指定数组索引处的元素。

    $arrayElemAt 具有以下语法:

    1. { $anyElement: [ <expression> ] }

    <array>表达式可以是任何有效的表达式,只要它可以解析为数组。

    <idx>表达式可以是任何有效表达式,只要它可以解析为整数。

    • 如果为正,则从数组开始算起$arrayElemAt返回该idx位置的元素 。
    • 如果为负,则从数组末尾算起$arrayElemAt返回该idx位置处的元素 。

    如果idx超过了数组界限,$arrayElemAt则不返回任何结果。

    有关表达式的更多信息,请参见 表达式。

    行为

    有关表达式的更多信息,请参见 表达式。

    例子 结果
    { $arrayElemAt: [ [ 1, 2, 3 ], 0 ] } 1
    { $arrayElemAt: [ [ 1, 2, 3 ], -2 ] } 2
    { $arrayElemAt: [ [ 1, 2, 3 ], 15 ] }

    例子

    名为的集合users包含以下文档:

    1. { "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] }
    2. { "_id" : 2, "name" : "li", favorites: [ "apples", "pudding", "pie" ] }
    3. { "_id" : 3, "name" : "ahn", favorites: [ "pears", "pecans", "chocolate", "cherries" ] }
    4. { "_id" : 4, "name" : "ty", favorites: [ "ice cream" ] }

    下面的示例返回favorites数组中的第一个和最后一个元素 :

    1. db.users.aggregate([
    2. {
    3. $project:
    4. {
    5. name: 1,
    6. first: { $arrayElemAt: [ "$favorites", 0 ] },
    7. last: { $arrayElemAt: [ "$favorites", -1 ] }
    8. }
    9. }
    10. ])

    该操作返回以下结果:

    1. { "_id" : 1, "name" : "dave123", "first" : "chocolate", "last" : "apples" }
    2. { "_id" : 2, "name" : "li", "first" : "apples", "last" : "pie" }
    3. { "_id" : 3, "name" : "ahn", "first" : "pears", "last" : "cherries" }
    4. { "_id" : 4, "name" : "ty", "first" : "ice cream", "last" : "ice cream" }

    译者:李冠飞

    校对: