在mongodb中进行过滤时获取数组的索引

时间:2018-12-12 18:05:22

标签: mongodb mongodb-query aggregation-framework

我将进行查询以获取子数组的索引为偶数的数据。

输入值:

{
    "_id" : ObjectId("5b3b206c4a25da19d05f41a2"),
    "numbers" : [ 
        {
            "name" : "Foobar",
            "lines" : [ 
                {
                    "number" : 6,
                }, 
                {
                    "number" : 11,

                },
                {
                    "number" : 15,

                },
                {
                    "number" : 8,
                }

            ]
        }
    ]
}

所需的输出:

{
    "_id" : ObjectId("5b3b206c4a25da19d05f41a2"),
    "numbers" : [ 
        {
            "name" : "Foobar",
            "lines" : [ 
                {
                    "index"  : 0,
                    "number" : 6,
                },
                {
                    "index"  : 3,
                    "number" : 8,
                }
            ]
        }
    ]
}

最重要的是获取“行”项的索引。

1 个答案:

答案 0 :(得分:1)

您需要使用$map遍历第一个数组,然后使用$filter遍历嵌套数组。

db.collection.aggregate([
  { "$project": {
    "numbers": {
      "$map": {
        "input": "$numbers",
        "as": "nn",
        "in": {
          "name": "$$nn.name",
          "lines": {
            "$filter": {
              "input": {
                "$map": {
                  "input": "$$nn.lines",
                  "as": "ll",
                  "in": {
                    "number": "$$ll.number",
                    "index": { "$indexOfArray": ["$$nn.lines.number", "$$ll.number"] }
                  }
                }
              },
              "as": "ll",
              "cond": { "$eq": [{ "$mod": ["$$ll.number", 2] }, 0] }
            }
          }
        }
      }
    }
  }}
])

即使您需要更多的聚合技巧

db.collection.aggregate([
  { "$project": {
    "numbers": {
      "$map": {
        "input": {
          "$map": {
            "input": "$numbers",
            "as": "nn",
            "in": {
              "name": "$$nn.name",
              "lines": {
                "$filter": {
                  "input": { "$zip": { "inputs": ["$$nn.lines", { "$range": [0, { "$size": "$$nn.lines" }] }] }},
                  "as": "ll",
                  "cond": {
                    "$let": {
                      "vars": { "num": { "$arrayElemAt": ["$$ll", 0] }},
                      "in": { "$eq": [{ "$mod": ["$$num.number", 2] }, 0] }
                    }
                  }
                }
              }
            }
          }
        },
        "as": "nn",
        "in": {
          "name": "$$nn.name",
          "lines": {
            "$map": {
              "input": "$$nn.lines",
              "as": "kk",
              "in": {
                "number": { "$arrayElemAt": ["$$kk.number", 0] },
                "index": { "$arrayElemAt": ["$$kk", 1] }
              }
            }
          }
        }
      }
    }
  }}
])

最后两者都会给您类似的Output

[
  {
    "_id": ObjectId("5b3b206c4a25da19d05f41a2"),
    "numbers": [
      {
        "name": "Foobar",
        "lines": [
          {
            "index": 0,
            "number": 6
          },
          {
            "index": 3,
            "number": 8
          }
        ]
      }
    ]
  }
]