mongodb aggregation 聚合

Aggregation Pipeline Behavior
In MongoDB, the aggregate command operates on a single collection, logically passing the entire collection into
the aggregation pipeline. To optimize the operation, wherever possible, use the following strategies to avoid scanning

the entire collection.

聚合管道表现

在Mongodb,聚合命令在单个collection上进行操作,逻辑上是吧整个collection传进聚合管道。为了优化操作,尽可能的在能用到的地方使用以下的策略去避免扫描整个collection。


Pipeline Operators and Indexes
The $match and $sort pipeline operators can take advantage of an index when they occur at the beginning of the
pipeline.
New in version 2.4: The $geoNear pipeline operator takes advantage of a geospatial index. When using $geoNear,
the $geoNear pipeline operation must appear as the first stage in an aggregation pipeline.
Even when the pipeline uses an index, aggregation still requires access to the actual documents; i.e. indexes cannot
fully cover an aggregation pipeline.

Changed in version 2.6: In previous versions, for very select use cases, an index could cover a pipeline.

管道操作以及索引

对于 $match and $sortd管道操作,当他们发生在管道的开始可以充分利用索引的优势。

在新的版本2.4中: $geoNear管道操作利用了地理空间索引geospatial,当使用geospatialgeospatial管道操作必须出现在聚合管道的第一步。

Early Filtering

If your aggregation operation requires only a subset of the data in a collection, use the $match, $limit, and $skip
stages to restrict the documents that enter at the beginning of the pipeline. When placed at the beginning of a pipeline,
$match operations use suitable indexes to scan only the matching documents in a collection.
Placing a $match pipeline stage followed by a $sort stage at the start of the pipeline is logically equivalent to a

single query with a sort and can use an index. When possible, place $match operators at the beginning of the pipeline.

早期过滤

如果你的聚合操作需要这个collection的数据子集,那就在管道的开始使用$match, $limit, and $skip策略去限制documents的进入。当放置在管道的开始,$match操作使用合适的索引去仅仅扫描collection中匹配的documents。在管道的开始把$match管道策略放置在 $sort之后,这在逻辑上等于单个可以使用索引的sort查询。可能的时候把$match放在管道的开始


Additional Features
The aggregation pipeline has an internal optimization phase that provides improved performance for certain sequences
of operators. For details, see Aggregation Pipeline Optimization (page 449).
The aggregation pipeline supports operations on sharded collections. See Aggregation Pipeline and Sharded Collections (page 452).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值