问题
$lookup is new in MongoDB 3.2. It performs a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing.
To use $lookup, the from collection cannot be sharded.
On the other hand, sharding is a useful horizontal scaling approach.
What's the best practise to use them together?
回答1:
As the docs you quote indicate, you can't use $lookup on a sharded collection. So the best practice workaround is to perform the lookup yourself in a separate query.
- Perform your
aggregatequery. - Pull the "localField" values from your query results into an array, possibly using Array#map.
- Perform a
findquery against the "from" collection, using a query like{foreignField: {$in: localFieldArray}} - Merge your results into whatever format you need.
Don't let the $lookup limitation stop you from sharding collections that require it for scalability, just perform the lookup function yourself.
来源:https://stackoverflow.com/questions/34633111/mongodb-to-use-sharding-with-lookup-aggregation-operator