javascript - 文本索引 MongoDB 的对象数组
问题描述
我已经浏览了解决方案,但在我的情况下似乎没有一个工作
我的问题是我有一个列表猫鼬模型,其中每个列表可以有许多类别,我希望能够通过类别搜索并返回与这些类别关联的所有列表。
我的模型是这样定义的
let ListingSchema = new Schema({
categories: [{
id: {type: Number, required: true},
name: {type: String, required: true},
suggestedName: {type: String, required: true},
link: {type: String, path: true}
}],
allergyAdvice: {type: String},
location: {type: String, required: true, maxlength: 100},
vendorId: {type: Schema.Types.ObjectId, ref: 'User', index: true},
}, {timestamps: true});
ListingSchema.index({
alergyAdvice: 'text',
'categories.name': 'text',
}, {
weights: {
'categories.name': 7,
alergyAdvice: 4,
},
});
ListingSchema.set('toObject', {getters: true});
ListingSchema.set('toJSON', {getters: true});
在搜索具有相关类别的列表时,我使用以下查询 -
const listings = await Listing.find({
$text: {$search: "Sweet"}
});
该查询适用于alergyAdvice 索引,但不适用于该索引,categories.name
并且会为列表返回一个空数组。
解决方案
推荐阅读
- python - 控制 Django 继承模型中的对象创建流程
- python - 使用 MatPlotLib 绘制单独的散点图
- python - 如何在 Sonarqube 中导入自定义模块?模块 YYY 中没有名称 XXX
- javascript - 如何在 d3 js 中对齐堆栈图中的条形图?
- python - Python:将编码为字节字符串(来自 PyTorch)的浮点数转换为 int
- python - 在字典列表中,根据键合并列表
- c - 如何按数组对c中的名称进行排序
- ruby - splat 运算符在应用于范围表达式时如何理解?
- google-cloud-platform - Cloud Tasks 条件执行
- maven - 如何允许结果
在 Kotlin 中作为返回类型?