+1. In the mean time, is there a way to globally disable auto index - versus having to disable it in each schema?
I use this in my schema definition options. It's not "global", but it works automatically. I don't think that mongoose should implement this automatically because you may edit your schema in dev (and everything was reIndex()ed for you) - but your production would break upon deployment. So, for example, we have a page that we pull up after every deployment which allows us to run tasks such as reIndexing. This could easily be automated, but we are at least aware that it needs to happen.
{ autoIndex: process.env.NODE_ENV !== 'production' }
Frankly, the fact that mongoose relies on env variables at all is a significant problem, and mongoose will not be doing so in the future. @wlingke I don't think there's a way to disable auto-index globally, but if there isn't I'll add it in for 4.0.
I haven't found any either, but am excited to see it in 4.0
+1. A global autoIndex setting would be handy and reduce error. As it is now, we have to remember to turn off autoIndex for every new schema we create.
Even better would be a global defaultOptions set that all schemas use as their starting set of options.
So https://github.com/LearnBoost/mongoose/commit/189e7353cb5a7da06518b79f264633fa1f4ecdf4 introduces an autoIndex
option on connections - if you don't explicitly set autoIndex
on a schema, models will use the underlying connection's autoIndex
option. You can use this option using:
var conn = mongoose.createConnection(uri, { autoIndex: false });
Or, if you're using the default connection,
mongoose.connect(uri, { autoIndex: false });
This will ensure that all models created over that connection don't call ensureIndexes()
Suggestions welcome. Thanks to @sr527 for working on this.
Sorry to comment on a closed issue, but the example given above is incorrect. The autoIndex setting needs to be inside a config
sub-object:
mongoose.connect(uri, { config: { autoIndex: false } });
This feature is great. Thanks!
Thanks for pointing that out.
@vkarpov15 and @rmarscher Global autoIndex option is not working ..below is my connection string
mongoose.connect(config.db.URL,{ config: { autoIndex: false } });
However in schema level it is working perfectly
UserSchema.set('autoIndex', false);
Below is my schema
UserSchema = new Schema({
username: {
type: String,
lowercase: true,
trim: true,
required: 'E-mail is required',
unique: 'The entered username is already exist'
},
email: {
type: String,
lowercase: true,
trim: true,
unique: 'The Email address you have entered already exists.',
uniqueCaseInsensitive:true,
required: 'E-mail is required',
validate: {
validator: function(email) {
return /^([\w-\.+]+@([\w-]+\.)+[\w-]{2,4})?$/.test(email);
},
message: '{VALUE} is not a valid email address'
}
},
});
Please let me know if any other options to set globally.
Woops looks like autoIndex
doesn't work with mongoose.connect()
, only mongoose.createConnection()
. Will fix, sorry for the trouble @yasharma
Actually my mistake. The below script works, see #5176
'use strict';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
mongoose.set('debug', true);
mongoose.connect('mongodb://localhost:27017', { config: { autoIndex: false } });
mongoose.model('Test', new Schema({ name: { type: String, unique: true } }));
Most helpful comment
Actually my mistake. The below script works, see #5176