猫鼬指数已经存在不同的选择 [英] mongoose index already exists with different options

查看:76
本文介绍了猫鼬指数已经存在不同的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用实施搜索结果视图。
我发现mongoose内部提供带有$ text的全文搜索功能。

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

我把下面的代码放到Post.js

I put the code below to Post.js

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

这是我在路由文件route / posts.js中放入的代码

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

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

我提出的错误信息低于

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.

推荐答案

检查您的文本索引定义在哪个字段。现在mongodb每个集合只允许一个文本索引。因此,如果您在desc列上定义了一个文本索引并尝试在其他列上使用该索引,则必然会出现此错误。

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

这篇关于猫鼬指数已经存在不同的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆