大家好,在过去的 16 个小时里,我正在尝试使用 mongoose 实现此搜索,但不幸的是,无论我尝试什么都不起作用。 希望这里有人能够帮助我。
所以我有架构
const propertySchema = new mongoose.Schema({
city:{
type:String
},
district:{
type:String
},
address:{
type:String
}
});
const Property = mongoose.model('property',propertySchema);
exports.module = Property
这是我的收藏(演示)数据
{
"city":"Prague",
"district":"district 1 ",
"address":address 1"
},
{
"city":"Prague",
"district":"district 1 ",
"address":address 1"
},
{
"city":"Ostrava",
"district":"district 2 ",
"address":address 2"
},
{
"city":"Ostrava",
"district":"district 2",
"address":address 2"
},
{
"city":"Brno",
"district":"district 3",
"address":address 1" //Let`s pretend somehow this address in Bruno exists in Prague too
},
我现在想要的是在搜索“布拉格”中写入以获得结果
Prague
例如,当我在搜索“地址 1”中写入以获取结果时
地址 1, District 1 , 布拉格 地址 1, District 3 , 布尔诺
我应该看起来像 https://www.zillow.com 搜索
到目前为止,我想出了很多想法,这是最后一个(没有用,但我仍然想与您分享,也许稍作修改就可以实现)
const all = await Property.find({$and:[{city:{$regex: req.body.tag, $options: 'i'}},{address:{$regex: req.body.tag, $options: 'i'}},{district:{$regex: req.body.tag, $options: 'i'}}]})
答案 0 :(得分:0)
在这种类型的场景中,您要将搜索输入与任何字段相匹配,您必须放置 $or
聚合而不是 $and
。因为您想将输入匹配为城市或地区或地址。