MongoDB链查找3个集合

时间:2018-01-17 14:12:38

标签: mongodb

我遇到以下情况的问题:

我有3个收藏品:

协议

{
   '_id': ObjectId('5a5c96be0e8945000fc6f504'),
   'condition': ObjectId('5a5c96be0e8945000fc6f503'),
   'created_at': datetime.datetime(2018, 1, 15, 11, 55, 42, 370000),
   'ends_at': None,
   'starts_at': None,
   'updated_at': None
}

agreement_conditions

{'__v': 0,
   '_id': ObjectId('5a5f424ffb065a000f4e7988'),
   'amount': 14860,
   'created_at': datetime.datetime(2018, 1, 17, 12, 32, 15, 900000),
   'currency': ObjectId('5a5c7858019f4e00015d8844'),
   'interest': 2.1,
   'interval': ObjectId('5a5c7858019f4e00015d8846'),
   'payback_interval': 4,
   'updated_at': None
}

agreement_intervals

  {'_id': ObjectId('5a5c7858019f4e00015d8846'), 'name': 'monthly'}

最后,我想要一个包含所有这些数据的集合:

     '_id': ObjectId('5a5c96be0e8945000fc6f504'),
     'condition': {
                '_id': ObjectId('5a5f424ffb065a000f4e7988'),
                'amount': 14860,
                'created_at': datetime.datetime(2018, 1, 17, 12, 32, 15, 900000),
                'currency': ObjectId('5a5c7858019f4e00015d8844'),
                'interest': 2.1,
                'interval': {'name': 'monthly'},
                'payback_interval': 4,
                'updated_at': None
},
     'created_at': datetime.datetime(2018, 1, 15, 11, 55, 42, 370000),
     'ends_at': None,
     'starts_at': None,
     'updated_at': None

我制作了两个聚合协议。对协议条件进行了一次聚合查询 和conditions.aggregate在agreement_intervals上查找但是然后我有2个命令器以及如何再次加入它们?

我很感激任何帮助! :)

1 个答案:

答案 0 :(得分:0)

感谢您的回答。我现在这样做了,它的工作原理。但任何改进的想法?

pipeline = agreements.aggregate([
    {"$lookup":
        {
            "from" : "agreement_conditions",
            "localField" : "condition",
            "foreignField" : "_id",
            "as" : "conditions"
        }
    },
    {"$unwind" : {"path": "$conditions", "preserveNullAndEmptyArrays": True}},
    {"$lookup" :
        {
            "from" : "agreement_intervals",
            "localField" : "conditions.interval",
            "foreignField" : "_id",
            "as" : "conditions.interval_type"
        }
    },
    {"$unwind" : "$conditions.interval_type"},
    {"$lookup" :
        {
            "from" : "agreement_currencies",
            "localField" : "conditions.currency",
            "foreignField" : "_id",
            "as" : "conditions.currency"
        }
    },
    {"$unwind" : "$conditions.currency"},
    {"$lookup":
        {
            "from" : "agreement_statuses",
            "localField" : "status",
            "foreignField" : "_id",
            "as" : "status"
        }
    },
    {"$unwind" : "$status"},
    {"$project":
        {
            "starts_at": 1,
            "ends_at": 1,
            "created_at": 1,
            "updatped_at": 1,
            "status": {"name": 1},
            "conditions": {
                "payback_interval": 1,
                "amount": 1,
                "interest": 1,
                "currency": {"iso_code": 1},
                "interval_type": {"name": 1}
                }
        }
    }
])