【问题标题】:Querying mongo array of embedded documents in aggregation pipeline在聚合管道中查询嵌入文档的 mongo 数组
【发布时间】:2018-03-28 00:34:07
【问题描述】:

我正在研究在聚合管道 MongoDB 中查询嵌入式文档数组的不同方法。看起来 MongoDB 对此的支持较少。

假设我们在测试集合中有以下文档:

/* 1 */
   {
    "_id" : ObjectId("59df2c39fbd406137d4290b3"),
    "a" : 1.0,
    "arr" : [ 
        {
           "key": 1,
           "sn" : "a",
           "org": "A"
        }
    ]
   }

/* 2 */
{
    "_id" : ObjectId("59df2c47fbd406137d4290b4"),
    "a" : 2.0,
    "arr" : [ 
        {
            "sn" : "b",
            "key": 2,
            "org": "B"
        }
    ]
}

/* 3 */
{
    "_id" : ObjectId("59df2c50fbd406137d4290b5"),
    "a" : 3.0,
    "arr" : [ 
        {

           "key": 3,
           "sn" : "c", 
           "org": "C"
        }
    ]
}

/* 4 */
{
    "_id" : ObjectId("59df2c85fbd406137d4290b6"),
    "a" : 1.0,
    "arr" : [ 
        {
           "key": 1,
           "sn" : "a",
           "org": " A"
        }
    ]
}

/* 5 */
{
    "_id" : ObjectId("59df2c9bfbd406137d4290b7"),
    "a" : 3.0,
    "arr" : [ 
        {
            "sn" : "b",
            "key": 2,
        }
    ]
}

/* 6 */
{
    "_id" : ObjectId("59df2e41fbd406137d4290b8"),
    "a" : 4.0,
    "arr" : [ 
        {
            "sn" : "b",
            "key" : 2
        }
    ]
}

/* 7 */
{
    "_id" : ObjectId("59df2e5ffbd406137d4290b9"),
    "a" : 5.0,
    "arr" : [ 
        {
           "key" : 2,
            "sn" : "b"
        }, 
        {
            "sn" : "a",
            "key" : 1
        }
    ]
}

我想使用以下查询根据“arr.sn”字段值对上述文档进行分类:

db.test.aggregate([{"$addFields": {"Category" : { $switch: {
     branches : [
       { case : { $eq : [ "$arr.nm", "a" ] }, then : "Category 1"}
    ],
    default : "No Category"
    }}}}])

但是 $eq 运算符没有给出正确的结果,如果我在 find 方法中使用相同的 $eq,它可以工作:

db.test.find({"arr.sn" : "a"})

我正在寻找仅使用单个字段的方法,例如“arr.sn”字段。有没有办法从数组中的嵌入文档中投影字段?

任何帮助将不胜感激。

【问题讨论】:

    标签: arrays mongodb aggregation-framework


    【解决方案1】:

    $eq(aggregation) 比较值和类型与查询 eq 操作符不同,后者可以比较任何类型的值。

    您需要$in(aggregation) 来验证数组中的值。

    类似

    [
      {
        "$addFields": {
          "Category": {
            "$switch": {
              "branches": [
                {
                  "case": {
                    "$in": [
                      "a",
                      "$arr.sn"
                    ]
                  },
                  "then": "Category 1"
                }
              ],
              "default": "No Category"
            }
          }
        }
      }
    ]
    

    【讨论】:

    • 感谢@Veeram 的解决方案,实际上 $in 运算符要求所有数组字段都存在,但是我正在寻找仅使用单个字段的方法,以防万一“arr.sn”场地。我已经修改了有问题的数组条目。有没有办法从数组中的嵌入文档中投影字段?
    • Np。我不跟着你。单场是什么意思? arr.sn 返回值数组,$in 在值数组中查找值。我错过了什么?
    • 您的解决方案有效,但不适用于数组包含以下嵌入文档的情况 例如:arr : [ { "key": 1, "sn" : "a", "org": " A" } ]。
    • 在 $in 子句中,我只想尝试使用 "sn" : "a"
    • 它适用于所有情况。当聚合查询管道中没有这样的语法时,不确定为什么要尝试 sn:a。在上述情况下,arr.sn 返回 ["a"],当你在 ["a"] 中执行 "a" 时,它返回 true。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 2021-09-23
    • 2019-08-01
    • 2013-12-06
    相关资源
    最近更新 更多