我正在尝试从填充的模型中搜索商品名称。我的模型如下:
export interface IIddwModel extends Document {
problemType: Schema.Types.ObjectId | IProblemTypeModel;
commonName: Schema.Types.ObjectId | ICommonNameModel;
scientificName: Schema.Types.ObjectId | IScientificNameModel;
causalAgentType: Schema.Types.ObjectId |ICausalAgentTypeModel;
}
const iddwSchema = new Schema(
{
problemType: {
type: Schema.Types.ObjectId,
ref: "ProblemType",
required: true
},
commonName: {
type: Schema.Types.ObjectId,
ref: "CommonName",
required: true
},
scientificName: {
type: Schema.Types.ObjectId,
ref: "ScientificName",
required: true
},
causalAgentType: {
type: Schema.Types.ObjectId,
ref: "CausalAgent",
required: true
}
},
{
timestamps: true
}
);
iddwSchema.index({ "$**": "text" });
我的控制器是
Iddw.find()
.populate([
{
path: "problemType",
model: ProblemType,
},
{
path: "commonName",
model: CommonName
},
{
path: "scientificName",
model: ScientificName
},
{
path: "causalAgentType",
model: CausalAgentType
}
])
.then(iddws => {
return res.status(200).json({
iddws: iddws
});
})
.catch((error: WriteError) => {
return res.status(500).json({
error: error.code
});
});
如何从填充的字段进行搜索? 我尝试过
if (req.query.search) {
query.$text = {
$search: req.query.search
};
Iddw.find(query)
.then(() => {}
但是它只能从IDDW模型一部分的字符串字段中搜索,而不能从问题字段,commonName,scientificName等填充文件中搜索。如何从这些填充字段中搜索?