Node / Mongo自动完成,搜索两个字段

时间:2018-01-13 22:47:21

标签: node.js mongodb autocomplete promise nosql

我正在使用Node / Express / Mongo / Mongoose / jQuery / jQuery UI自动完成。我有一系列看起来像这样的企业:

[
    {
    name: "Example Corporation",
    "address": {
        "city": "New York",
        "state: "NY"
    },
    {
    name: "ABC Corp",
    "address": {
        "city": "Atlanta",
        "state: "GA"
    },
    {
    name: "ACME Inc",
    "address": {
        "city": "Dallas",
        "state: "TX"
    },
    {
    name: "New York Company",
    "address": {
        "city": "New York",
        "state: "NY"
    },
    {
    name: "XYZ Co",
    "address": {
        "city": "Newark",
        "state: "NJ"
    }
}
]

我想创建一个自动完成功能,允许有人输入商家名称或城市的部分名称,然后将结果分组,如下所示:

如果我搜索"New"我想要:

{
    businesses: [
       {
       name: "New York Company",
       address: {
           "city": "New York",
           "state": "NY"
           }
       },
       {
       name: "XYZ Co",
       address: {
           "city": "Newark",
           "state": "NJ"
           }
       }
    ],
    cities: [
       {
           "city": "New York",
           "state": "NY"
       },
       {
           "city": "Newark",
           "state": "NJ"
       }
    ]
}

非常类似于您可以在OpenTable上看到的自动完成搜索,您可以在此处按餐厅或位置进行搜索。 我希望返回唯一的城市名称。

我是否需要按照商家名称和城市搜索两次集合,并在最后将它们组合在一起?我很困惑。我不需要ID回来,因为最终结果他们要么去城市页面或商业页面。我希望这是有道理的。

0 个答案:

没有答案