简体   繁体   English

获取元素的索引 mongodb 聚合

[英]Get index of an element mongodb aggregation

Here is my collection这是我的收藏

 { "_id" : ObjectId("5c225f9a66d39d55c036fa66"), "name" : "Sherlock", "mobile" : "999999", "adress" : [ { "street" : "221b baker street", "city" : "london" }, { "street" : "ben street", "city" : "london" } ], "tags" : [ "Detective", "Magician", "Avenger" ] }

Now I want to get the first or second value inside address array.现在我想获取地址数组中的第一个或第二个值。 for that I'm using this command.为此,我正在使用此命令。

 > db.agents.findOne({"name" : "Sherlock"},{"adress" : 1})

but instead of giving a single result it is giving the entire array like但不是给出单个结果,而是给出整个数组,如

 { "_id" : ObjectId("5c225f9a66d39d55c036fa66"), "adress" : [ { "street" : "221b baker street", "city" : "london" }, { "street" : "ben street", "city" : "london" } ] }

It can be done by comparing array value like它可以通过比较数组值来完成

 db.agents.find({"adress.street": "ben street"}, {_id: 0, 'adress.$': 1});

But I don't want to compare just to print the array indexes.但我不想仅仅为了打印数组索引而进行比较。 How can I get the single result?我怎样才能得到单一的结果? Any help is appreciated..任何帮助表示赞赏..

You can $unwind with includeArrayIndex to get the index of address array您可以使用includeArrayIndex $unwind来获取地址数组的索引

db.t11.aggregate([
    {$match : {"adress.street" : "ben street"}},
    {$unwind : {path : "$adress", includeArrayIndex : "idx"}},
    {$match : {"adress.street" : "ben street"}}
]).pretty()

you can add $project to filter the fields not required您可以添加$project来过滤不需要的字段

result结果

> db.t11.aggregate([{$match : {"adress.street" : "ben street"}},{$unwind : {path : "$adress", includeArrayIndex : "idx"}},{$match : {"adress.street" : "ben street"}}]).pretty()
{
        "_id" : ObjectId("5c225f9a66d39d55c036fa66"),
        "name" : "Sherlock",
        "mobile" : "999999",
        "adress" : {
                "street" : "ben street",
                "city" : "london"
        },
        "tags" : [
                "Detective",
                "Magician",
                "Avenger"
        ],
        "idx" : NumberLong(1)
}
>

You can use $arrayElemAt to get the specific element from the array您可以使用$arrayElemAt从数组中获取特定元素

db.collection.aggregate([
  { $addFields: { "$arrayElemAt": ["$adress", 0] }}   //index
])

and if you want to get the sliced element then you can use $slice projection如果你想获得切片元素,那么你可以使用$slice投影

db.collection.find({}, { adress: { $slice: [2, 1] }})  // 2 index and 1 number of element

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在MongoDb中按特定索引获取元素 - How to get element by specific index in MongoDb 如何提取MongoDB聚合中数组元素的字段? - How to extract the field of an array element in MongoDB Aggregation? MongoDB - 更新数组中刚刚创建的元素(聚合) - MongoDB - Update a just created element in an array (aggregation) 如何在Mongodb上的旧版本中获取数组元素的索引? - How to get the index of an array element in older versions on Mongodb? 使用管道(聚合或更新)将项目推入特定索引处的数组 - mongodb - push item into array at a specific index with pipeline (aggregation or update) - mongodb MongoDB 聚合用于查找具有 $gt 条件的嵌套 object 数组的索引 - MongoDB aggregation for finding an index of array nested object with $gt condition 为数组字段的每个元素计算数学公式-Mongodb Aggregation Framework - Calculate a Mathematic formula for each element of array field - Mongodb Aggregation Framework MongoDB通过NodeJS中的索引更新文档的数组元素 - MongoDB update array element of a document by index in nodeJS 如何使用聚合 mongoDB 从 2 个集合中获取链接数据 - How to get linked data from 2 collections with aggregation mongoDB MongoDB 聚合:从数组中的日期/时间戳获取日、月和年 - MongoDB aggregation : get day, month and year from date/timestamp in array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM