Reputation: 175
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.
Upvotes: 0
Views: 426
Reputation: 175
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.
Upvotes: 0