我有一个需要在对象数组中执行搜索的架构。我已经尝试了很多方法,但是似乎没有用。下面是我的代码。
let productsSchema = new mongooseSchema({
productId: {type: mongooseSchema.Types.ObjectId, ref: 'Product'},
mainAttributes: [{
subCategoryId: {type: mongooseSchema.Types.ObjectId, ref: 'CategorySubCategory'},
attributes: Object
}],
createdAt: Date,
updatedAt: Date
})
productsSchema.index({'mainAttributes': 'text'})
let attributes = mongoose.model('Attributes', productsSchema);
let results = await CategorySubCategory.attributes.find({"$text": {"$search": req.body.searchText}})
当前下面的记录保存在mongodb中
{
"_id" : ObjectId("5bba1b39ad9387431f9f5fd9"),
"productId" : ObjectId("5bba1b397d90713320c441f8"),
"__v" : 0,
"createdAt" : ISODate("2018-10-07T14:42:01.723Z"),
"mainAttributes" : [
{
"_id" : ObjectId("5bba1b397d90713320c441f9"),
"subCategoryId" : ObjectId("5bba1b397d90713320c441f7"),
"attributes" : {
"title" : {
"text" : "some title",
"type" : "text"
}
"state" : {
"text" : "California",
"type" : "text"
},
"city" : {
"text" : "San Francisco",
"type" : "text"
}
}
},
{
"_id" : ObjectId("5bba1b397d90713320c441fb"),
"subCategoryId" : ObjectId("5bba1b397d90713320c441fa"),
"attributes" : {
"temprature" : {
"degree" : "20C",
"type" : "text"
}
}
}
],
"updatedAt" : ISODate("2018-10-07T14:42:01.723Z")
}
我需要根据文本进行搜索,例如当我给San时,它应该返回结果,但返回空值。
答案 0 :(得分:0)
我已经弄清楚了问题所在,
我需要显式删除索引,然后添加
?
使其正常工作