Javascript MongoDB索引搜索在控制台中工作,但不在浏览器中工作
所有这些昨天都起作用了,所以我真的不确定出了什么问题。我已经搜索了很多次文档和步骤,但是我还没有找到它。如有任何帮助,我们将一如既往地感谢 如果我在shell中键入Javascript MongoDB索引搜索在控制台中工作,但不在浏览器中工作,javascript,node.js,mongodb,indexing,mongoose,Javascript,Node.js,Mongodb,Indexing,Mongoose,所有这些昨天都起作用了,所以我真的不确定出了什么问题。我已经搜索了很多次文档和步骤,但是我还没有找到它。如有任何帮助,我们将一如既往地感谢 如果我在shell中键入db.movies.find({'$text':{'$search':'elephant'}}),它将返回标题中带有“elephant”的标题列表。但是,如果我从浏览器中运行相同的搜索,它会使节点服务器崩溃,我会得到$text query所需的MongoError:text index 错误来自find()的错误响应: my DB上的
db.movies.find({'$text':{'$search':'elephant'}})
,它将返回标题中带有“elephant”的标题列表。但是,如果我从浏览器中运行相同的搜索,它会使节点服务器崩溃,我会得到$text query所需的MongoError:text index
错误来自find()
的错误响应:
my DB上的索引如下所示:
{
"v" : 2,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "title_text",
"ns" : "db.movies",
"weights" : {
"title" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
}
我还定义了mongoose模式,并使用一个自动递增插件在每次电影保存时递增一个自定义id
字段。(保存电影有效,因此我正在连接到DB ok)
正如@Chris Satchell在评论中指出的那样 将索引添加到mongoose模式可以解决这个问题 而不是:
mongoose.model('Job', new Schema({
id: Number,
title: String,
desc: String,
genre: String
})
这样做:
mongoose.model('Job', new Schema({
id: Number,
title: { type: String, text: true },
desc: String,
genre: String
})
即使我在模式上仍然有
.index({title:'text'})
,您仍然需要在属性上定义{type:String,text:true}
。第二组参数{score…}
是什么?find()
有一个排序()
,它使用$meta:'textcore'
,它还必须在find中定义为arg。我将更新问题以包含完整对象,而不是变量是否在shell中运行完全相同的查询(带排序)?您声明可以在shell中运行查询,但没有提到传递给Movie.find
的第二个参数对象。这会给你带来麻烦吗?试试看
mongoose.model('Job', new Schema({
id: Number,
title: String,
desc: String,
genre: String
})
mongoose.model('Job', new Schema({
id: Number,
title: { type: String, text: true },
desc: String,
genre: String
})