我正在尝试在前端构建搜索栏。我希望此搜索栏可以遍历对象,如果找到匹配的对象,则返回找到的值。
基本上我有这个:
theSchema.find({
$and: [
{ company: String(company)},
{ user: String(user) },
{
$or: [
{ 'information.field1': { $regex: String(search), $options: "i" } },
{ 'information.field2': { $regex: String(search), $options: "i" } },
{ 'information.field3': { $regex: String(search), $options: "i" } },
{ 'information.field4': { $regex: String(search), $options: "i" } },
]
}
]
})
.then(result => result)
这是我的模式:
const theSchema = new Schema({
...
information: {
type: Schema.Types.Mixed,
default: {}
},
...
})
所以我试图遍历information
对象并搜索其中的所有字段。
由于明显的原因,我目前拥有的东西有些不切实际。我不想每次将新字段添加到对象时都修改搜索,并且并非架构中的所有文档都具有所有字段。
我尝试查看$in
运算符,但这似乎适用于数组。
有什么方法可以搜索对象中的所有字段?