Mongoose compound indexes not being created (Too long)
Asked Answered
G

1

6

I'm having some issues creating multiple compound indexes on one of my schemas in MongoDB. When using MongoLab, I know some indexes are not created when they are too long, due to a naming issue. So I suspect this might be the reason why some are not created

var Schema = new mongoose.Schema({ ... });

// Created
Schema.index({
    one: 1,
    three: 1
});

// Not Created
Schema.index({
    one: 1,
    two: 1,
    three: 1,
    four: 1,
    five: 1,
    six: 1,
    seven: 1,
    eight: 1,
    nine: 1,
    ten: 1
});
Galloromance answered 3/2, 2016 at 2:18 Comment(5)
does it print some thing to the screen when create 2nd index ? how do you know that the second index is not created ?Did you check in mongo shell ?Induction
@Induction I didn't add any logging yet. I did check the mongo shell and it wasn't created, hence how I know. When I manually try to create that index, i get the following: "The default index name generated by MongoDB is too long. Please specify a custom name."Galloromance
Your right, there is a limitation to the default index name (docs.mongodb.org/manual/reference/limits/#Index-Name-Length) You can specify custom name by passing addition option to Schema.index Schema.index({ one: 1, two: 1, three: 1, four: 1, five: 1, six: 1, seven: 1, eight: 1, nine: 1, ten: 1 }, { name: 'my_index_name' } );Induction
Thanks, that was exactly what I was looking forGalloromance
@Induction Can you add that as an answer, then I can mark it as solvedGalloromance
I
8

There is a limitation to the default index name (https://docs.mongodb.org/manual/reference/limits/#Index-Name-Length)

You can specify custom index name by passing addition option object to Schema.index

Schema.index({
    one: 1,
    two: 1,
    three: 1,
    four: 1,
    five: 1,
    six: 1,
    seven: 1,
    eight: 1,
    nine: 1,
    ten: 1 }, 
    { name: 'my_index_name' }
);
Induction answered 3/2, 2016 at 4:20 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.