1

I'm trying to create an index in mongobd. However, i'm getting an error message that states that the index name is too long (127 byte max)

Error message

{
        "ok" : 0,
        "errmsg" : "namespace name generated from index name \"dictionary.words.$word_text_conjugation.indicative.presente_text_conjugation.indicative.preterite_text_conjugation.indicative.imperfect_text_conjugation.indicative.conditional_text_conjugation.indicative.futuro_text_conjugation.imperativo.afirmativo_text_conjugation.imperativo.negativo_text_conjugation.subjuntivo.presente_text_conjugation.subjuntivo.imperfect_text_conjugation.subjuntivo.imperfect2_text_conjugation.subjuntivo.futuro_text_conjugation.infinitivo_text_conjugation.gerundio_text_conjugation.participio_text\" is too long (127 byte max)",
        "code" : 67,
        "codeName" : "CannotCreateIndex"
}

The objects being stored looks like(fields not being indexed are removed for clarity)

let verbObject = {
    word: "comer",
    pos: {
            conjugation: {
                indicative: {
                    presente: ["como", "comes", "come", "comemos", "coméis", "comen"],
                    preterite: ["comí", "comiste", "comió", "comimos", "comisteis", "comieron"],
                    imperfect: ["comía", "comías", "comía", "comíamos", "comíais", "comían"],
                    conditional: ["comería", "comerías", "comería", "comeríamos", "comeríais", "comerían"],
                    futuro: ["comeré", "comerás", "comerá", "comeremos", "comeréis", "comerán"]
                },
                imperativo: {
                    afirmativo: ["come", "coma", "comamos", "comed", "coman"],
                    negativo: ["no comas", "no coma", "no comamos", "no comáis", "no coman"]
                },
                subjuntivo:{
                    presente: ["coma", "comas", "coma", "comamos", "comáis", "coman"],
                    imperfect: ["comiera", "comieras", "comiera", "comiéramos", "comierais", "comieran"],
                    imperfect2: ["comiese", "comieses", "comiese", "comiésemos", "comieseis", "comiesen"],
                    futuro: ["comiere", "comieres", "comiere", "comiéremos", "comiereis", "comieren"],
                },
                infinitivo: "comer",
                gerundio: "comiendo",
                participio: "comido",
            }
        },
}

According to this answer the obvious solution is to shorten the index name.

The problem

.1 I don't want to change the names as it makes it less reader friendly and I would have to rewrite a lot of code to match the new object design.

  1. the shortest naming scheme possible (not counting white spaces) is 304 bytes.

The shortest possible naming scheme

db.words.createIndex({
     "w": "text",
     "p.v.c.in.p" : "text",
     "p.v.c.in.pt" : "text",
     "p.v.c.in.i" : "text",
     "p.v.c.in.c" : "text",
     "p.v.c.in.f" : "text",
     "p.v.c.im.a" : "text",
     "p.v.c.im.n" : "text",
     "p.v.c.s.p" : "text",
     "p.v.c.s.i" : "text",
     "p.v.c.s.i2" : "text",
     "p.v.c.s.f" : "text",
     "p.v.c.i": "text",
     "p.v.c.g": "text",
     "p.v.c.p": "text",
})

How can I create an indexing for my database design?

  • 1
    You can use [Wildcard Text Indexes](https://docs.mongodb.com/manual/core/index-text/index.html#wildcard-text-indexes). From the docs: _When creating a text index on multiple fields, you can also use the wildcard specifier ($**). With a wildcard text index, MongoDB indexes every field that contains string data for each document in the collection..._. That should solves the issue. – prasad_ Dec 25 '19 at 07:05

0 Answers0