获取MongoDB中数组字段中给定元素的索引

Jam*_*ang 6 mapreduce mongodb mongodb-query aggregation-framework

想想这个MongoDB文档:

{_id:123, "food":[ "apple", "banana", "mango" ]}
Run Code Online (Sandbox Code Playgroud)

问题:如何获得mango食物的位置?

查询应该2在上面返回,并且不返回整个文档.

请显示工作查询.

Bla*_*ven 5

除了使用之外,确实没有其他方法(“服务器端”)mapReduce

db.collection.mapReduce(
    function() {
        emit(this._id, this.food.indexOf("mango"));
    },
    function() {},   // reducer never gets called since all _id is unique
    { 
        "out": { "inline": 1 },
        "query": { "food": "mango" }
    }
)
Run Code Online (Sandbox Code Playgroud)

除了文档本身之外,它是唯一会以修改后的形式返回其他内容的东西,并且使用所需的 JavaScript 评估来确定答案,

不幸的是,没有“本地”运营商可以做到这一点。

除非您出于真正的聚合目的需要此操作,否则在处理“每个文档”的基础上时,最好在客户端的本机代码中执行类似的“数组索引匹配”。


sty*_*ane 5

从MongoDB版本3.4开始,我们可以使用$indexOfArray运算符返回可以在数组中找到给定元素的索引.

$indexOfArray有三个论点.第一个是以$符号为前缀的数组字段的名称.

第二个是元素,第三个是可选的开始搜索的索引.$indexOfArray如果未指定开始搜索的索引,则返回找到该元素的第一个索引.


演示:

> db.collection.insertOne( { "_id" : 123, "food": [ "apple", "mango", "banana", "mango" ] } )
{ "acknowledged" : true, "insertedId" : 123 }
> db.collection.aggregate( [ { "$project": { "matchedIndex": { "$indexOfArray": [ "$food", "mango" ] } } } ] )
{ "_id" : 123, "matchedIndex" : 1 }
> db.collection.aggregate( [ { "$project": { "matchedIndex": { "$indexOfArray": [ "$food", "mango", 2 ] } } } ] )
{ "_id" : 123, "matchedIndex" : 3 }
> db.collection.aggregate( [ { "$project": { "matchedIndex": { "$indexOfArray": [ "$food", "apricot" ] } } } ]  )
{ "_id" : 123, "matchedIndex" : -1 }
Run Code Online (Sandbox Code Playgroud)

  • 如何使用表达式作为第二个参数来匹配对象值?例如 `{ $indexOfArray: ['$array', { $and: [...]}]}` ?使用 $and 似乎不起作用。似乎想要匹配物体的精确形状 (2认同)