使用另一个字段的值查找 MongoDB 个对象

Find MongoDB object using value of another field

我最近发现很难找到存储在文档中且其键位于同一文档的另一个字段中的对象。

{
    list : {
        "red" : 397n8,
        "blue" : j3847,
        "pink" : 8nc48,
        "green" : 983c4,
    },
    result : [
                { "id" : 397n8, value : "anger" },
                { "id" : j3847, value : "water" },
                { "id" : 8nc48, value : "girl" },
                { "id" : 983c4, value : "evil" }
             ]
    }
}

我正在尝试获取 'blue' 的值,它的 ID 为 'j3847',值为 'water'。

db.docs.find( { result.id : list.blue }, { result.value : 1 } );

# list.blue would return water
# list.pink would return girl
# list.green would return evil

我尝试了很多东西,甚至找到了一篇关于如何使用同一文档中的值更新值的好文章。:Update MongoDB field using value of another field 我自己基于此;没有成功...:/

如何使用另一个字段的值找到 MongoDB 对象?

一种方法是使用 $where 运算符,但不推荐使用它,因为无论其他什么条件可能使用索引选择,它都会调用完整的集合扫描,并且还会调用 JavaScript 解释器每个结果文档,这将比本机代码慢得多。

也就是说,使用替代 .aggregate() 方法进行此类比较,这绝对是更好的选择:

db.docs.aggregate([
    { "$unwind": "$result" },
    {
        "$project": {
            "result": 1,
            "same": { "$eq": [ "$list.blue", "$result.id" ] }
        }
    },
    { "$match": { "same": true } },
    { 
        "$project": {
            "_id": 0,
            "value": "$result.value"
        }
    }
])

$unwind operator is applied on the result array field, it will generate a new record for each and every element of the result field on which unwind is applied. It basically flattens the data and then in the subsequent $project步骤检查数组的每个成员以比较两个字段是否相同。

示例输出

{
    "result" : [ 
        {
            "value" : "water"
        }        
    ],
    "ok" : 1
}

另一种选择是使用$map and $setDifference operators in a single $project step where you can avoid the use of $unwind,这在非常大的集合上可能代价高昂,并且在大多数情况下会导致 16MB BSON 限制约束:

db.docs.aggregate([
    {
        "$project": {
            "result": { 
                "$setDifference": [
                    { 
                        "$map": {
                            "input": "$result",
                            "as": "r",
                            "in": { 
                                "$cond": [
                                    { "$eq": [ "$$r.id", "$list.blue" ] },
                                    "$$r",
                                    false
                                ]
                            }
                        }
                    },
                    [false]
                ]
            }
        }
    }
])

示例输出

{
    "result" : [ 
        {
            "_id" : ObjectId("569412e5a51a6656962af1c7"),
            "result" : [ 
                {
                    "id" : "j3847",
                    "value" : "water"
                }
            ]
        }
    ],
    "ok" : 1
}

您可以在 mongo 聚合中使用 $filter 运算符。它 returns 一个仅包含符合条件的元素的数组:

db.docs.aggregate([
  {  
    $project: {
      result: {
        $filter: {
          input: "$result", 
          as:"item", 
          cond: { $eq: ["$list.blue", "$$item.id"]}
        }
      }
    }
  }
])

此查询的输出如下所示:

{ 
  "_id" : ObjectId("569415c8299692ceedf86573"), 
  "result" : [ { "id" : "j3847", "value" : "water" } ] 
}