猫鼬:如何综合使用索引? [英] Mongoose: how to use index in aggregate?

查看:64
本文介绍了猫鼬:如何综合使用索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何汇总使用索引?

我看到了文档 https://docs.mongodb.com/manual/core/aggregation-pipeline/#pipeline-operators-and-indexes

$ match和$ sort管道运算符可以在索引出现在管道开头时加以利用.

The $match and $sort pipeline operators can take advantage of an index when they occur at the beginning of the pipeline.

有没有使用索引的方法而不是开始的情况?

Is there any way of using index not the beginning situation?

$sort一样, $match$group

请帮助我

推荐答案

索引的作用是保留指向集合中给定记录的某些数据的记录.认为这就像一本小说,然后是一张纸,上面列出了该小说中各个人物或地点的名称,并带有提及他们的页码.

An index works by keeping a record of certain pieces of data that point to a given record in your collection. Think of it like having a novel, and then having a sheet of paper that lists the names of various people or locations in that novel with the page numbers where they're mentioned.

聚合就像拿那本小说,然后将不同的页面转换成完全不同的信息流一样.在转换真正发生之前,您不知道新信息的位置,因此您不可能在转换后的信息上建立索引.

Aggregation is like taking that novel and transforming the different pages into an entirely different stream of information. You don't know where the new information is located until the transformation actually happens, so you can't possibly have an index on that transformed information.

换句话说,不可能在不是一开始的任何聚合管道阶段都使用索引,因为该数据将被转换,并且MongoDB无法知道是否有可能有效地利用新的索引.转换后的数据.

In other words, it's impossible to use an index in any aggregation pipeline stage that is not at the very beginning because that data will have been transformed and MongoDB has no way of knowing if it's even possible to efficiently make use of the newly transformed data.

如果聚合管道太大而无法有效处理,则需要以某种方式限制管道的大小,以便可以更有效地处理它.理想情况下,这意味着具有$match阶段,该阶段足以将文档限制为合理大小的子集.但是,这并非总是可能的,因此可能需要付出更多的努力.

If your aggregation pipeline is too large to handle efficiently, then you need to limit the size of your pipeline in some way such that you can handle it more efficiently. Ideally this would mean having a $match stage that sufficiently limits the documents to a reasonably-sized subset. This isn't always possible, however, so additional effort may be required.

一种可能性是生成摘要"文档,这些文档是将所有新数据聚合在一起的结果,然后仅使用这些摘要文档执行主聚合管道.例如,如果您希望汇总系统中的交易记录,则可以生成每日摘要,记录当日已记录的不同交易的数量和类型,以及其他任何附加数据需要.然后,您可以将汇总管道限制为仅这些每日摘要文档,而避免使用常规的交易文档.

One possibility is generating "summary" documents that are the result of aggregating all new data together, then performing your primary aggregation pipeline using only these summary documents. For example, if you have a log of transactions in your system that you wish to aggregate, then you could generate a daily summary of the quantities and types of the different transactions that have been logged for the day, along with any other additional data you would need. You would then limit your aggregation pipeline to only these daily summary documents and avoid using the normal transaction documents.

但是,实际的解决方案不在此问题的范围内.请注意,索引的使用是您无法避免的限制.

An actual solution is beyond the scope of this question, however. Just be aware that the index usage is a limitation that you cannot avoid.

这篇关于猫鼬:如何综合使用索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆