Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensureIndex #3156

Closed
ORESoftware opened this issue Jul 11, 2015 · 2 comments
Closed

ensureIndex #3156

ORESoftware opened this issue Jul 11, 2015 · 2 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@ORESoftware
Copy link

I have this code in my app:

 userSchema = mongoose.Schema({
        role: {
            type: String,
            enum: ['Admin', 'Owner', 'User']
        },
        username: {
            type: String,
            unique: true,
            required: true,
            validate: [validation.usernameValidator, 'not a valid username']
        },
        passwordHash: {
            type: String,
            required: true,
            validate: [validation.passwordValidator, 'not a valid password']
        },
        email: {
            type: String,
            unique: true,
            required: true,
            validate: [validation.emailValidator, 'not a valid email address']
        }
    },
        {
            autoIndex:false
        });

var UserModel = null;

var get = function (cb) {

    if(UserModel === null){
        UserModel = mongoDB.model('users', userSchema);
        UserModel.ensureIndexes(function(err){
            if(err){
                console.log(colors.bgRed(err));
            }
            cb(err,UserModel);
        });
    }
    else{
        cb(null,UserModel);
    }
};

two requests from me to you:

(1) can you clarify in the docs what the benefits of setting autoIndex to false?
(2) can you clarify in the docs if ensureIndexes fires once for each index or once for all indexes?

thanks!

@vkarpov15
Copy link
Collaborator

  1. Sometimes you'd prefer to manage indexes in production via the shell or some other tool. Mongoose is not designed to be a tool for managing mongodb indexes and ensureIndex is a primitive mechanism.
  2. Mongoose calls ensureIndex() once for each index sequentially, and then emits an 'index' event on the model when either the index build succeeds or when one of the ensureIndex() calls fails.

@vkarpov15 vkarpov15 added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Jul 11, 2015
@vkarpov15 vkarpov15 added this to the 4.0.8 milestone Jul 11, 2015
@ORESoftware
Copy link
Author

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

2 participants