mongodb 获取MongoDB中数组的第n个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7223273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:13:10  来源:igfitidea点击:

Get n-th element of an array in MongoDB

mongodbmongodb-queryaggregation-framework

提问by mck

As part of my document in MongoDB I'm storing an array of objects. How can I query it for only the 4th element of the array for example? So I don't want the get the entire array out, just the 4th element.

作为我在 MongoDB 中的文档的一部分,我存储了一个对象数组。例如,如何仅查询数组的第 4 个元素?所以我不想取出整个数组,只取出第 4 个元素。

回答by Russell

Use $slice.

使用$slice.

db.foo.find({ bar : "xyz" } , { my_array : { $slice : [n , 1] } } )

will retrieve the nth element of the array "my_array" of all documents in the foo collection where bar = "xyz".

将检索 foo 集合中所有文档的数组“my_array”的第 n 个元素,其中 bar =“xyz”。

Some other examples from the MongoDB documentation:

MongoDB 文档中的其他一些示例:

db.posts.find({}, {comments:{$slice: 5}}) // first 5 comments
db.posts.find({}, {comments:{$slice: -5}}) // last 5 comments
db.posts.find({}, {comments:{$slice: [20, 10]}}) // skip 20, limit 10
db.posts.find({}, {comments:{$slice: [-20, 10]}}) // 20 from end, limit 10

Which you can read here: http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields

您可以在这里阅读:http: //www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields

回答by styvane

You can use the $arrayElemAtoperator new in MongoDB 3.2 to return the element at the specified array index.

您可以使用$arrayElemAtMongoDB 3.2 中的运算符 new 返回指定数组索引处的元素。



Demo:

演示:

A collection named basketscontains documents that look like this:

名为baskets的集合包含如下所示的文档:

{
    "_id" : ObjectId("578f326f6db61a299a383c5a"),
    "fruits" : [
        "apple",
        "mango",
        "banana",
        "apricot",
        "cherry"
    ]
}

The following query return the element at index -2(second element) in the "fruits" array.

以下查询返回-2“fruits”数组中索引(第二个元素)处的元素。

db.baskets.aggregate(
    [
        { "$project": { "matched": { "$arrayElemAt": [ "$fruits", 1 ] } } } 
    ] 
)

which produces

产生

{ 
    "_id" : ObjectId("578f326f6db61a299a383c5a"), 
    "matched" : "mango" 
}

And the following query the element before the last element in the array; thus the element at index -2

下面查询数组中最后一个元素之前的元素;因此索引处的元素-2

db.baskets.aggregate(
    [
        { "$project": { "matched": { "$arrayElemAt": [ "$fruits", -2 ] } } } 
    ] 
)

which yields:

产生:

{ 
    "_id" : ObjectId("578f326f6db61a299a383c5a"), 
    "matched" : "apricot" 
}

回答by Eskoala

Another way to do this is to use the update array syntax. Here, contribs.1sets the second element in the contribsarray to have value ALGOL 58(Taken from the manual page on update syntax)

另一种方法是使用更新数组语法。在这里,contribs.1contribs数组中的第二个元素设置为具有值ALGOL 58(取自更新语法手册页

db.bios.update(
   { _id: 1 },
   { $set: { 'contribs.1': 'ALGOL 58' } }
)