Mongoose 对象中的猫鼬搜索
我正试图在我的前端建立一个搜索栏。我希望这个搜索栏在一个对象中循环,如果它找到匹配项,则返回找到的值 基本上我有:Mongoose 对象中的猫鼬搜索,mongoose,schema,Mongoose,Schema,我正试图在我的前端建立一个搜索栏。我希望这个搜索栏在一个对象中循环,如果它找到匹配项,则返回找到的值 基本上我有: theSchema.find({ $and: [ { company: String(company)}, { user: String(user) }, { $or: [ {
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: {}
},
...
})
因此,我试图循环遍历我的信息对象并搜索其中的所有字段
由于明显的原因,我目前所拥有的有点不切实际。我不希望每次向对象添加新字段时都修改搜索,并且模式中并非所有文档都包含所有字段
我试着在
操作符中查看$in,但这似乎对数组有效
有没有办法搜索对象中的所有字段?在这种情况下,可能需要使用聚合在这种情况下,可能需要使用聚合