Reputation: 777
I can not understand one thing. As we read in Moongose docs:
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 theautoIndex
option of your schema tofalse
.
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?
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.
Upvotes: 2
Views: 1690
Reputation: 97
in cause you might want build special index that mongoose's schema doesn't support, eg: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 in this cause you have to call ensureIndex() yourself
Upvotes: 0
Reputation: 33185
They probably could have gone either way with that setting default. It probably should just be disabled by default, and then they wouldn't have to make the disclaimer that you should disable it in production.
As for 2. If you've got a billion records to index and you add a new index in your model without realizing it's not in production, this will kill your server for a day.
Upvotes: 1
Reputation: 43884
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.
I personally actually make all indexes in the shell only now.
Upvotes: 2