Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

mongodb - mongomock aggregate $lookup does not resolve collection name like real mongo db (python)

I have a simple aggregate lookup query like this:

cursor = db.my_db['shop'].aggregate([
        {
            '$lookup':
                {
                    'from': 'customer',
                    'localField': 'in_shop',
                    'foreignField': '_id',
                    'as': 'joined_customers'
                }
        }
    ])

Note that this query uses the other collection customer it its from clause. This query works perfectly fine against an actual mongo db but not against my mongomock.MognoClient object.

To get it to work with the mongomock I need to add the database prefix to the query like so:

cursor = db.my_db['shop'].aggregate([
        {
            '$lookup':
                {
                    'from': 'my_db.customer',
                    'localField': 'in_shop',
                    'foreignField': '_id',
                    'as': 'joined_customers'
                }
        }
    ])

However if I do that this query does not work against the actual mongo DB anymore. What is going wrong here? I can't create code that works on both the mock and the actual db.

If I don't include the database name the mock does not find the other collection to join it. To be clear both queries work perfectly fine on their respective "systems" and both collections can be queried in the mock on their own. Just the lookup of the customer collection does not work correctly in the mongomock if the from clause does not include the database name prefix.

question from:https://stackoverflow.com/questions/65883608/mongomock-aggregate-lookup-does-not-resolve-collection-name-like-real-mongo-db

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found the issue. I mocked the imported package instead of the database in the imported package. This has effectively resulted in something like Database(Database(Collection())) whilst it should be only Database(Collection()). So it did not have anything to do with mongomock after all.

I guess the learning is that nested databases are possible and since they behave just like variables in an imported package the error did not show until an internal function tried to cross-reference another Collection.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...