繁体   English   中英

如何推进 mongoose 搜索?

[英]How to advance mongoose search?

大家好,过去 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, 区 1, 布拉格 地址 1, 区 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'}}]})

在这种类型的场景中,您希望将搜索输入与任何字段匹配,您必须使用$or聚合而不是$and 因为您希望将输入匹配为城市或地区或地址。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM