mongoose index already exists with different options

前端 未结 1 1787
清歌不尽
清歌不尽 2021-01-15 02:49

I am implementing search result view to my app. I figured out that mongoose internally provide full text search function with $text.

I put the code below to Post.js<

相关标签:
1条回答
  • 2021-01-15 03:10

    check on which field you have your text index defined. Right now mongodb allows only one text index per collection. so if you have defined a text index on desc column and try to use that index on some other column you are bound to get this error.

    can you try to query your index and see on which column you created it. To get indexes you can do

    db.collection.getIndexes()
    

    and it will return something like this

    [
        {
            "v" : 1,
            "key" : {
                "_id" : 1
            },
            "name" : "_id_",
            "ns" : "some.ns"
        },
        {
            "v" : 1,
            "key" : {
                "_fts" : "text",
                "_ftsx" : 1
            },
            "name" : "desc_text",
            "ns" : "some.ns",
            "weights" : {
                "title" : 1
            },
            "default_language" : "english",
            "language_override" : "language",
            "textIndexVersion" : 2
        }
    ]
    

    now if you want to scope in other columns also to use this index simply drop this index

    db.collection.dropIndex('desc_text');
    

    and then recreate it by including all columns you want to be covered by text index,

    db.collection.createIndex({
        title:'text;,
        body: 'text;,
        desc: 'text',
        ...... and so on
    });
    
    0 讨论(0)
提交回复
热议问题