【问题标题】:Fetch data from Array in MongoDB with elemMatch使用 elemMatch 从 MongoDB 中的数组中获取数据
【发布时间】:2017-02-10 07:59:43
【问题描述】:

我在产品集合中有以下文档。

db.product.find()
{ "_id" : 1, "results" : [ { "product" : "abc", "score" : 10 }, { "product" : "xyz", "score" : 5 } ] }
{ "_id" : 2, "results" : [ { "product" : "abc", "score" : 8 }, { "product" : "xyz", "score" : 7 } ] }
{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "xyz", "score" : 8 } ] }

以下查询使用 elemMatch 返回结果。

> db.product.find(  {  results :  { $elemMatch :   { product : "xyz" , score : { $eq : 5}  }  } }   )
{ "_id" : 1, "results" : [ { "product" : "abc", "score" : 10 }, { "product" : "xyz", "score" : 5 } ] }

同样,这也会返回预期的结果。

> db.product.find(  {  results :  {    product : "xyz" , score : 5  }   }   )
{ "_id" : 1, "results" : [ { "product" : "abc", "score" : 10 }, { "product" : "xyz", "score" : 5 } ] }

但是当我在数组中使用比较运算符时,我没有得到任何结果。

db.product.find(  {  results :  {    product : "xyz" , score : { $eq : 5}  }   }   )

我无法弄清楚这种意外行为。

【问题讨论】:

    标签: arrays mongodb mongodb-query


    【解决方案1】:

    查询有两种方式:传递子文档和使用点符号。

    要查询嵌套字段,您应该使用点表示法。这意味着您需要这样做:

    db.test.find({"results.product": "xyz", "results.score": {$eq : 5}})

    如果你传递一个子文档,那么 mongoDB 会进行完全匹配。这意味着不应该有任何额外的属性,基本上在你的情况下,它期望分数是 {"$eq" : 5} (字面上有名为 $eq 的属性)

    更多信息请查看this answerdocumentation

    【讨论】:

      【解决方案2】:

      我已经运行了所有 4 个命令的解释。 以下是 mongoDB 在内部创建的过滤器:

      db.product.find({ results : { product : "xyz" , score : 5  }   }).explain()
      "filter" : {
                      "results" : {
                          "$eq" : {
                              "product" : "xyz",
                              "score" : 5
                          }
                      }
                  }
      
      
      db.product.find({ "results.product" : "xyz" , "results.score" : 5   }).explain()
            "filter" : {
                          "$and" : [
                              {
                                  "results.product" : {
                                      "$eq" : "xyz"
                                  }
                              },
                              {
                                  "results.score" : {
                                      "$eq" : 5
                                  }
                              }
                          ]
                      }
      
      db.product.find(  {  results :  { $elemMatch :   { product : "xyz" , score : { $eq : 5}  }  } }   ).explain()
                "filter" : {
                              "results" : {
                                  "$elemMatch" : {
                                      "$and" : [
                                          {
                                              "product" : {
                                                  "$eq" : "xyz"
                                              }
                                          },
                                          {
                                              "score" : {
                                                  "$eq" : 5
                                              }
                                          }
                                      ]
                                  }
                        }
                      }
      
      db.product.find(  {  results :  {    product : "xyz" , score : { $eq : 5}  }   }   ).explain()
      
                          "filter" : {
                                      "results" : {
                                          "$eq" : {
                                              "product" : "xyz",
                                              "score" : {
                                                  "$eq" : 5
                                              }
                                          }
                                      }
                                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-03
        • 2017-05-28
        • 2015-05-15
        • 1970-01-01
        • 2015-06-22
        • 1970-01-01
        • 2023-01-14
        相关资源
        最近更新 更多