mongoose index already exists with different options

余生长醉 提交于 2019-12-12 07:19:55

问题


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

PostSchema.index({desc: 'text'}); //for example

Here's the code I put in my routing file route/posts.js

Post.find({$text: {$search : 'please work!'}}).exec(function (err, posts) {...}) 

The error message I come up with is below

Index with pattern: { _fts: "text", _ftsx: 1 } already exists with different options

Would there any body who know how to deal with this error and figure out? Thanks you.


回答1:


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
});


来源:https://stackoverflow.com/questions/41150675/mongoose-index-already-exists-with-different-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!