javascript - mongoose index already exists with different options - Stack Overflow

I am implementing search result view to my app.I figured out that mongoose internally provide full tex

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 e 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.

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 e 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.

Share Improve this question asked Dec 14, 2016 at 19:30 supergentlesupergentle 1,0911 gold badge15 silver badges34 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744202522a4562948.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信