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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…