加入MongoDb后如何使用匹配?

时间:2016-09-05 10:21:43

标签: mongodb aggregation-framework

我想在mongodb中查找后过滤嵌套属性。我在文档中找不到合适的例子。我使用聚合,在最后阶段我必须通过几个条件。我希望按查找阶段收紧的属性进行过滤。

这是我的疑问:

        db.collection('leagues').aggregate([
                    {
                        $match: {
                            _id: ObjectID(leagueId)
                        }
                    },
                    {
                        $lookup: {
                            from: "league_configs",
                            localField: "_id",
                            foreignField: "leagueId",
                            as: "configs"
                        }
                    },
                    {
                        $lookup: {
                            from: "events",
                            localField: "_id",
                            foreignField: "leagueId",
                            as: "events"
                        }
                    },
                    {
                        $match: {
                            $and: [{
                                "events": {
                                    "isCalculated": {
                                        $eq: false
                                    },
                                    "isDeleted": {
                                        $eq: false
                                    },
                                    "startDate": {
                                        $lte: new Date()
                                    }
                                },
                            }]
                        }
                    }
            ])

联盟对象示例:

{
"_id" : ObjectId("57c6bf5934db7e18b8af650c"),
"name" : "League1",
"adminId" : ObjectId("57c5a08b8857ad1da099e8de"),
"isStarted" : false,
"isFinished" : false,
"users" : [ 
    ObjectId("57c5a08b8857ad1da099e8de")
],
"isPublic" : true,
"createdDate" : "2016-08-31T13:28:25+02:00",
"updatedDate" : ISODate("2016-08-31T12:28:09.064Z"),
"currentStage" : 1,
"isDeleted" : false
}

示例事件对象:

{
"_id" : ObjectId("57c6bfca34db7e18b8af650f"),
"home" : "home",
"guest" : "guest",
"description" : null,
"result" : "X",
"resultHome" : 1,
"resultGuest" : 1,
"startDate" : ISODate("2016-08-31T18:30:00.000Z"),
"type" : "1",
"specialPoints" : null,
"leagueStage" : 1,
"isDeleted" : false,
"isCalculated" : false,
"leagueId" : ObjectId("57c6bf5934db7e18b8af650c"),
"updatedDate" : ISODate("2016-09-02T06:52:45.544Z")
}

有任何建议吗?

1 个答案:

答案 0 :(得分:2)

这是查询。您可以使用“$ elemMatch”来过滤“events”集合中的数据。此外,为了清楚起见,我将查找结果重命名为“events_docs”而不是“event”。

请注意,我已删除了“league_configs”上的第一个查找,因为我没有该收集数据。您可以添加它并尝试完整查询。

以下查询可以很好地处理您提供的数据。

<强>查询: -

db.leagues.aggregate([
                    {
                        $match: {
                            _id: ObjectId("57c6bf5934db7e18b8af650c")
                        }
                    },
                       {
                        $lookup: {
                            from: "events",
                            localField: "_id",
                            foreignField: "leagueId",
                            as: "events_docs"
                        }
                    },
                    {
                        $match: { "events_docs": {$elemMatch: {"isDeleted" : false,"isCalculated" : false, "startDate": {
                                        $lte: new Date()
                                    }}  } }

                    }

            ]);

<强>输出: -

{
    "_id" : ObjectId("57c6bf5934db7e18b8af650c"),
    "name" : "League1",
    "adminId" : ObjectId("57c5a08b8857ad1da099e8de"),
    "isStarted" : false,
    "isFinished" : false,
    "users" : [ 
        ObjectId("57c5a08b8857ad1da099e8de")
    ],
    "isPublic" : true,
    "createdDate" : "2016-08-31T13:28:25+02:00",
    "updatedDate" : ISODate("2016-08-31T12:28:09.064Z"),
    "currentStage" : 1,
    "isDeleted" : false,
    "events_docs" : [ 
        {
            "_id" : ObjectId("57c6bfca34db7e18b8af650f"),
            "home" : "home",
            "guest" : "guest",
            "description" : null,
            "result" : "X",
            "resultHome" : 1,
            "resultGuest" : 1,
            "startDate" : ISODate("2016-08-31T18:30:00.000Z"),
            "type" : "1",
            "specialPoints" : null,
            "leagueStage" : 1,
            "isDeleted" : false,
            "isCalculated" : false,
            "leagueId" : ObjectId("57c6bf5934db7e18b8af650c"),
            "updatedDate" : ISODate("2016-09-02T06:52:45.544Z")
        }
    ]
}

测试案例2: - 我为联盟id = ObjectId(“57ce74076eae72aafab03013”)运行了相同的查询。该查询获取了零文档。

联盟文件: -

{
    "_id" : ObjectId("57ce74076eae72aafab03013"),
    "name" : "League1",
    "adminId" : ObjectId("57c5a08b8857ad1da099e8de"),
    "isStarted" : false,
    "isFinished" : false,
    "users" : [ 
        ObjectId("57c5a08b8857ad1da099e8de")
    ],
    "isPublic" : true,
    "createdDate" : "2016-08-31T13:28:25+02:00",
    "updatedDate" : ISODate("2016-08-31T12:28:09.064Z"),
    "currentStage" : 1,
    "isDeleted" : false
}

活动文件: -

此活动没有任何联盟身份。

{
    "_id" : ObjectId("57ce74256eae72aafab03014"),
    "home" : "home",
    "guest" : "guest",
    "description" : null,
    "result" : "X",
    "resultHome" : 1,
    "resultGuest" : 1,
    "startDate" : ISODate("2016-08-31T18:30:00.000Z"),
    "type" : "1",
    "specialPoints" : null,
    "leagueStage" : 1,
    "isDeleted" : false,
    "isCalculated" : false,
    "updatedDate" : ISODate("2016-09-02T06:52:45.544Z")
}

此事件具有联盟ID(ObjectId(“57ce74076eae72aafab03013”)),但isDeleted和isCalculated属性设置为true。因此,查询没有获取此文档。

{
    "_id" : ObjectId("57ce74ea6eae72aafab03015"),
    "home" : "home",
    "guest" : "guest",
    "description" : null,
    "result" : "X",
    "resultHome" : 1,
    "resultGuest" : 1,
    "startDate" : ISODate("2016-08-31T18:30:00.000Z"),
    "type" : "1",
    "specialPoints" : null,
    "leagueStage" : 1,
    "isDeleted" : true,
    "isCalculated" : true,
    "leagueId" : ObjectId("57ce74076eae72aafab03013"),
    "updatedDate" : ISODate("2016-09-02T06:52:45.544Z")
}