简体   繁体   中英

Testing $lookup aggregations with Mongomock

I got an aggregation query with $lookup inside it:

pipeline = [{
    '$match': {
        '_id': ObjectId(layout_id)
    }
}, {
    '$lookup': {
        'from': 'units',
        'localField': 'unit_id',
        'foreignField': '_id',
        'as': 'layout_unit'
    }
}, {
    '$replaceRoot': {
        'newRoot': {
            '$mergeObjects': [{
                '$arrayElemAt': ["$layout_unit", 0]
            }]
        }
    }
}, {
    '$project': {
        'layout_unit': 0
    }
}, {
    '$lookup': {
        'from': 'users',
        'localField': 'user_id',
        'foreignField': '_id',
        'as': 'unit_user'
    }
}, {
    '$unwind': '$unit_user'
}]

I would like to unit-test it using mongomock . The issue here is, that as of version 3.9.0 it doesn't support $lookup aggregations.

NotImplementedError: Although '$lookup' is a valid operator for the aggregation pipeline, it is currently not implemented in Mongomock.

Is there a workaround to this? Or perhaps a substitute solution to mongomock ?

Maybe a few months late but because of old version in my computer I discovered that now the lib released an implementation for that operator.

Pull request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM