如何编写以下MongoDB查询

问题描述 投票:1回答:1

假设我有以下foobar集合:

{
  "foobar": [
    {
      "_id": "abc",
      "history": [
        {
          "type": "foo",
          "timestamp": 123456789.0
        },
        {
          "type": "bar",
          "timestamp": 123456789.0
        }
      ]
    },
    {
      "_id": "dfg",
      "history": [
        {
          "type": "baz",
          "timestamp": 123456789.0
        },
        {
          "type": "bar",
          "timestamp": 123456789.0
        }
      ]
    },
    {
      "_id": "hij",
      "history": [
        {
          "type": "foo",
          "timestamp": 123456789.0
        },
        {
          "type": "bar",
          "timestamp": 123456789.0
        },
        {
          "type": "foo",
          "timestamp": 123456789.0
        }
      ]
    }
  ]
}

如何查询($gte / $ltefoobar项目基于他们的timestamp项目中的history道具,但使用timestamp项目中的type: "foo"

如果没有type等于foo的子文档,则整个文档被过滤掉,如果有多个子文档type等于foo那么它可以匹配任何人。

mongodb nosql aggregation-framework
1个回答
1
投票

您可以尝试以下聚合:

var threshold = 123456788;
db.foobar.aggregate([
    {
        $addFields: {
            foobar: {
                $filter: {
                    input: "$foobar",
                    as: "doc",
                    cond: {
                        $let: {
                            vars: { 
                                foo: {
                                    $arrayElemAt: [
                                        {
                                            $filter: {
                                                input: "$$doc.history",
                                                as: "history",
                                                cond: {
                                                    $eq: [ "$$history.type", "foo" ]
                                                }
                                            }
                                        },
                                        0]
                                }
                            },
                            in: {
                                $gt: [ "$$foo.timestamp", threshold ]
                            }
                        }
                    }
                }
            }
        }
    }
])

$addFields可用于覆盖现有字段(foobar)。要筛选出与您的条件不匹配的所有子文档,您可以使用$filter(外部文档)。对于每个foobar文档,您可以使用$let来定义临时变量foo。你需要内部$filter来获得所有history元素,其中typefoo然后$arrayElemAt获得第一个。然后你只需要$gt$lt来应用你的比较。

对于那些没有foo的子文档,将返回undefined,然后在$gt阶段过滤掉。

© www.soinside.com 2019 - 2024. All rights reserved.