Mongoose call sureIndex at startup, but it is not recommended. So why is it defaul?

I can not understand anything. As we read in Moongose โ€‹โ€‹Docs:

When the application starts, Mongoose automatically calls ensureIndex for each specific index in your schema. Although this is good for development, it is recommended that this behavior be disabled during production, since creating an index can produce significant results. Disable the behavior by setting the autoIndex parameter of your schema to false .

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

  • Shouldn't this method always be called? If we want to use the index, and we are not sure whether MongoDb created one of them, why do we have the opportunity to use it, and is it not difficult to code in Mongoose so that we always have and index it?

I probably misunderstood something, so I will be grateful that you directly put me.

+2
source share
3 answers

We test in development and guarantee that our indexes will work in production?

Damn it, imagine that a random change has occurred in your programming, meaning that an index spanning multiple shards and replicas needs to be rebuilt.

It is better to have this in development than production.

Shouldn't this method always be called?

No, accidental reconfiguration and rebuilding of indexes due to errors / typos is a real threat since I recognized myself once.

I personally actually do all the indexes in the shell just now.

+2
source

They could probably go anyway with this default setting. This should probably just be disabled by default, and then they would not have to refuse that you should disable it during production.

As for 2. If you have a billion records for indexing, and you add a new index to your model, not realizing that it is not in production, this will kill your server for a day.

+1
source

because you might want to create a special index that does not support the mongoose schema, for example: text search index โ€œtextโ€, you can use

 Model.collection.ensusreIndex( { title: "text", tags: "text", description: "text" }, { weights: { title: 10, tags: 5, }, name: "TextIndex" },function(err,data){ if(err){ return res.send({success:false,err:err}) }else{ return res.send({success:true,res:'successfuly build index'}) }; }) ) 

so for this reason you need to call securityIndex () yourself

0
source

Source: https://habr.com/ru/post/1239911/


All Articles