猫鼬在启动时调用sureIndex,但不建议这样做.那么为什么要默认呢? [英] Mongoose call ensureIndex on startup, but it's not recommended. So why it's defaul?

查看:65
本文介绍了猫鼬在启动时调用sureIndex,但不建议这样做.那么为什么要默认呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解一件事.正如我们在详细的文档中所看到的:

I can not understand one thing. As we read in Moongose docs:

当您的应用程序启动时,Mongoose会自动为架构中的每个已定义索引调用ensureIndex.虽然对开发有利,但建议在生产中禁用此行为,因为创建索引会严重影响性能.通过将架构的autoIndex选项设置为false来禁用该行为.

When your application starts up, Mongoose automatically calls ensureIndex for each defined index in your schema. While nice for development, it is recommended this behavior be disabled in production since index creation can cause a significant performance impact. Disable the behavior by setting the autoIndex option of your schema to false.

  1. 因此,在开发中默认使用ensureIndex有什么意义,如果最好在生产模式(更敏感的位置)将其禁用.不应该是相反的吗?我们在开发中进行测试,并确保我们的索引在生产中有效?

  1. So what's the point of using ensureIndex by default in development, if there is preferable to disable it in production mode(more sensitive place). Shouldn't it be the oposite? We test in development and ensure that our indexes works in production?

该方法不是总是被调用吗?如果我们要使用索引,并且不确定MongoDb是否创建了索引,为什么我们可以选择使用它,而Mongoose中没有对此进行硬编码,以确保我们始终拥有索引?

Shouldn't this method be called always? If we want to use index, and we're not sure if MongoDb created one, why we have option to use this, and this is not hard coded in Mongoose, to ensure we always have and index?

我可能误会了一些东西,所以我会很高兴将我摆平.

I probably misunderstood something, so I'll be gratefull for putting me straight.

推荐答案

我们在开发中进行测试,并确保我们的索引在生产中有效?

We test in development and ensure that our indexes works in production?

不,想象一下您的编程中发生了意外更改,这意味着必须重建覆盖多个分片和副本集的索引.

Hell no, imagine an accidental change occured in your programming that meant an index covering multiple shards and replicasets has to be rebuilt.

发生在开发中而不是在生产中发生.

Better to have that happen in development than production.

不是总是要调用此方法吗?

Shouldn't this method be called always?

不,我一次发现自己由于错误/错别字而意外地重新配置和重建索引是一个真正的威胁.

No, accidental reconfiguration and rebuilding of indexes due to bugs/typos is a real threat as I found out myself once.

我个人实际上只是现在才在shell中建立所有索引.

I personally actually make all indexes in the shell only now.

这篇关于猫鼬在启动时调用sureIndex,但不建议这样做.那么为什么要默认呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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