如何使用$ filter获取数组字段的过滤元素

时间:2017-06-22 07:20:40

标签: node.js mongodb mongoose aggregation-framework

我在MongoDB中有一个火车集合,它有以下几行:

{
        "_id" : ObjectId("594a441fcbd8f815dc42436d"),
        "trainnum" : "011Ф",
        "startplace" : "Tashkent",
        "destination" : "Samarkand",
        "weeks" : [
                "1",
                "2",
                "3"
        ],
        "vagons" : [
                {
                        "_id" : ObjectId("594a6a42231e5912342ff903"),
                        "numvag" : "01K",
                        "seatcount" : "30",
                        "reserves" : [
                                {
                                        "numseats" : "1-10",
                                        "numreserve" : "1",
                                        "_id" : ObjectId("594a6a42231e5912342ff904")
                                }
                        ],
                        "date2" : ISODate("2017-06-30T00:00:00Z"),
                        "date1" : ISODate("2017-06-05T00:00:00Z")
                },
                {
                        "_id" : ObjectId("594a6c0d231e5912342ff905"),
                        "numvag" : "02C",
                        "seatcount" : "01-10",
                        "reserves" : [
                                {
                                        "numseats" : "01-10",
                                        "numreserve" : "05",
                                        "_id" : ObjectId("594a6c0d231e5912342ff906")
                                }
                        ],
                        "date2" : ISODate("2017-06-21T00:00:00Z"),
                        "date1" : ISODate("2017-06-25T00:00:00Z")
                }
        ],
        "date2" : ISODate("2017-07-30T00:00:00Z"),
        "date1" : ISODate("2017-06-01T00:00:00Z"),
        "__v" : 0
}
{
        "_id" : ObjectId("594a69c4231e5912342ff900"),
        "trainnum" : "012A",
        "startplace" : "Tashkent",
        "destination" : "Samarkand",
        "updated_at" : ISODate("2017-06-21T12:42:44.893Z"),
        "weeks" : [
                "1",
                "2",
                "3",
                "4"
        ],
        "vagons" : [
                {
                        "_id" : ObjectId("594b54fe0925d110180abf7b"),
                        "numvag" : "01K",
                        "seatcount" : "30",
                        "reserves" : [ ],
                        "date2" : ISODate("2017-07-23T00:00:00Z"),
                        "date1" : ISODate("2017-06-30T00:00:00Z")
                },
                {
                        "_id" : ObjectId("594b55ede16d1908b8129b42"),
                        "numvag" : "02L",
                        "seatcount" : "40",
                        "reserves" : [ ],
                        "date2" : ISODate("2017-07-11T00:00:00Z"),
                        "date1" : ISODate("2017-06-29T00:00:00Z")
                }
        ],
        "date2" : ISODate("2017-07-30T00:00:00Z"),
        "date1" : ISODate("2017-06-01T00:00:00Z"),
        "__v" : 0
}

如何检索所有具有date1(vagons date1)>的阴道的火车?当天(22.06.2017)(Vagons.date1>当天)。结果应该是这样的:

{
"_id" : ObjectId("594a441fcbd8f815dc42436d"),
"trainnum" : "011Ф",
"startplace" : "Tashkent",
"destination" : "Samarkand"        
"weeks" : [
            "1",
            "2",
            "3"
    ],
    "vagons" : [
            {
                    "_id" : ObjectId("594a6c0d231e5912342ff905"),
                    "numvag" : "02C",
                    "seatcount" : "01-10",
                    "reserves" : [
                            {
                                    "numseats" : "01-10",
                                    "numreserve" : "05",
                                    "_id" : ObjectId("594a6c0d231e5912342ff906")
                            }
                    ],
                    "date2" : ISODate("2017-06-21T00:00:00Z"),
                    "date1" : ISODate("2017-06-25T00:00:00Z")
            }
    ],
    "date2" : ISODate("2017-07-30T00:00:00Z"),
    "date1" : ISODate("2017-06-01T00:00:00Z"),
    "__v" : 0
}
`{
    "_id" : ObjectId("594a69c4231e5912342ff900"),
    "trainnum" : "012A",
    "startplace" : "Tashkent",
    "destination" : "Samarkand",
    "updated_at" : ISODate("2017-06-21T12:42:44.893Z"),
    "weeks" : [
            "1",
            "2",
            "3",
            "4"
    ],
    "vagons" : [
            {
                    "_id" : ObjectId("594b54fe0925d110180abf7b"),
                    "numvag" : "01K",
                    "seatcount" : "30",
                    "reserves" : [ ],
                    "date2" : ISODate("2017-07-23T00:00:00Z"),
                    "date1" : ISODate("2017-06-30T00:00:00Z")
            },
            {
                    "_id" : ObjectId("594b55ede16d1908b8129b42"),
                    "numvag" : "02L",
                    "seatcount" : "40",
                    "reserves" : [ ],
                    "date2" : ISODate("2017-07-11T00:00:00Z"),
                    "date1" : ISODate("2017-06-29T00:00:00Z")
            }
    ],
    "date2" : ISODate("2017-07-30T00:00:00Z"),
    "date1" : ISODate("2017-06-01T00:00:00Z"),
    "__v" : 0
}`

1 个答案:

答案 0 :(得分:1)

只需在字段上应用$filter条件即可。使用$gt作为"cond"参数。

如果您没有MongoDB 3.4或更高版本,请使用$project代替$addFields,只需在投影中包含所有其他字段:

Train.aggregate([
  { "$match": {
    "vagons.date1": { "$gte": new Date("2017-06-22") }
  }},
  { "$addFields": {
    "vagons": {
      "$filter": {
        "input": "$vagons",
        "as": "v",
        "cond": { "$gt": [ "$$v.date1", new Date("2017-06-22") ] }
      }
    }
  }}
])

给出输出:

{
        "_id" : ObjectId("594a441fcbd8f815dc42436d"),
        "trainnum" : "011Ф",
        "startplace" : "Tashkent",
        "destination" : "Samarkand",
        "weeks" : [
                "1",
                "2",
                "3"
        ],
        "vagons" : [
                {
                        "_id" : ObjectId("594a6c0d231e5912342ff905"),
                        "numvag" : "02C",
                        "seatcount" : "01-10",
                        "reserves" : [
                                {
                                        "numseats" : "01-10",
                                        "numreserve" : "05",
                                        "_id" : ObjectId("594a6c0d231e5912342ff906")
                                }
                        ],
                        "date2" : ISODate("2017-06-21T00:00:00Z"),
                        "date1" : ISODate("2017-06-25T00:00:00Z")
                }
        ],
        "date2" : ISODate("2017-07-30T00:00:00Z"),
        "date1" : ISODate("2017-06-01T00:00:00Z"),
        "__v" : 0
}
{
        "_id" : ObjectId("594a69c4231e5912342ff900"),
        "trainnum" : "012A",
        "startplace" : "Tashkent",
        "destination" : "Samarkand",
        "updated_at" : ISODate("2017-06-21T12:42:44.893Z"),
        "weeks" : [
                "1",
                "2",
                "3",
                "4"
        ],
        "vagons" : [
                {
                        "_id" : ObjectId("594b54fe0925d110180abf7b"),
                        "numvag" : "01K",
                        "seatcount" : "30",
                        "reserves" : [ ],
                        "date2" : ISODate("2017-07-23T00:00:00Z"),
                        "date1" : ISODate("2017-06-30T00:00:00Z")
                },
                {
                        "_id" : ObjectId("594b55ede16d1908b8129b42"),
                        "numvag" : "02L",
                        "seatcount" : "40",
                        "reserves" : [ ],
                        "date2" : ISODate("2017-07-11T00:00:00Z"),
                        "date1" : ISODate("2017-06-29T00:00:00Z")
                }
        ],
        "date2" : ISODate("2017-07-30T00:00:00Z"),
        "date1" : ISODate("2017-06-01T00:00:00Z"),
        "__v" : 0
}