项目后无法匹配

时间:2019-05-06 01:26:30

标签: mongodb mongodb-query aggregation-framework aggregate

我正在尝试在汇总查询中匹配项目。

我在管道上的项目查询之后添加了匹配查询。

匹配非查找字段有效,但匹配不适用于查找字段。

这有效:

aggregate_query = [
            {"$lookup": {
                "from": "users_db",
                "as": "customers",
                "localField": "user_id",
                "foreignField": "_id"
            }},
            {"$unwind": {
                "path": "$customers",
                "preserveNullAndEmptyArrays": True,
            }},
            {"$lookup": {
                "from": "accounts_db",
                "as": "accounts",
                "localField": "account_id",
                "foreignField": "_id"
            }},
            {"$unwind": "$accounts"},
            {"$project": {
                "display_username": "$customers.username",
                "display_account_username": "$accounts.username",
                "display_action": 1,
                "timezone": "$customers.timezone",
                "date": 1,
                "user_id": 1,
                "target_id": 1,
                "target_id2": 1,
                "source_ip": 1
            }},
            {'$match':{'$and':[{'source_ip':{'$regex':'127.0.0.1', '$options':'i'}}]}}
        ]

这不起作用(当有匹配的文档时返回空):

aggregate_query = [
            {"$lookup": {
                "from": "users_db",
                "as": "customers",
                "localField": "user_id",
                "foreignField": "_id"
            }},
            {"$unwind": {
                "path": "$customers",
                "preserveNullAndEmptyArrays": True,
            }},
            {"$lookup": {
                "from": "accounts_db",
                "as": "accounts",
                "localField": "account_id",
                "foreignField": "_id"
            }},
            {"$unwind": "$accounts"},
            {"$project": {
                "display_username": "$customers.username",
                "display_account_username": "$accounts.username",
                "display_action": 1,
                "timezone": "$customers.timezone",
                "date": 1,
                "user_id": 1,
                "target_id": 1,
                "target_id2": 1,
                "source_ip": 1
            }},
            {'$match':{'$and':[{'display_username':{'$regex':'ABC', '$options':'i'}}]}}
        ]

0 个答案:

没有答案