MongoDB将通过$ lookup聚合运算符使用分片 [英] MongoDB to Use Sharding with $lookup Aggregation Operator
问题描述
$lookup
是MongoDB 3.2中的新增功能.它对同一数据库中未分片的集合执行左外部联接,以从联接"的集合中筛选文档以进行处理.
$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.
要使用$lookup
,不能对from
集合进行分片.
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?
推荐答案
正如引用的文档所示,您不能在分片集合上使用$lookup
.因此,最佳实践的解决方法是在单独的查询中自行执行查找.
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.
- 执行您的
aggregate
查询. - 可能使用find查询
- 将结果合并为所需的任何格式.
- Perform your
aggregate
query. - Pull the "localField" values from your query results into an array, possibly using
Array#map
. - Perform a
find
query against the "from" collection, using a query like{foreignField: {$in: localFieldArray}}
- Merge your results into whatever format you need.
不要让$lookup
限制阻止您对需要扩展性的集合进行分片,而要自己执行查找功能.
Don't let the $lookup
limitation stop you from sharding collections that require it for scalability, just perform the lookup function yourself.
这篇关于MongoDB将通过$ lookup聚合运算符使用分片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!