代码之家  ›  专栏  ›  技术社区  ›  Marc Rasmussen

所有字段的环回搜索

  •  7
  • Marc Rasmussen  · 技术社区  · 7 年前

    loopback

    例如,采用以下表格字段:

    id, name, title, created, updated
    

    nomad 他说:“这是一个很好的选择。”

    我尝试过:

    {"where": {"keywords": {"inq": ["nomad"]}}}
    

    如果重要的话,我的数据库是 postgresql

    4 回复  |  直到 7 年前
        1
  •  5
  •   Pierre Clocher    7 年前

    text 在PostgreSQL数据库中建立索引。通过将所有需要的属性编入文本索引,您将能够在数据库中执行搜索。

    https://www.postgresql.org/docs/9.5/static/textsearch.html

    你仍然可以通过使用环回ORM来实现你的目标

    {"where": {"prop1": {"regex": "nomad"}, "prop2": {"regex": "nomad"}}}

        2
  •  3
  •   antzshrek    7 年前

    例子

    Fields: [id, name, title, created, updated]
    
    let whereCondition = {id: {regexp: '/nomad/i'}, name: {regexp: '/nomad/i'}}
    
    app.models.ModelName.find({
      where: whereCondition
    });
    

    https://loopback.io/doc/en/lb2/Where-filter.html#regular-expressions

        3
  •  1
  •   antzshrek    7 年前

    您将需要一个查询函数,该函数将遍历您的数据库并从表中返回您的数据。

    Account.find({where: {name: 'John'}, limit: 3}, function(err, accounts) { /* ... */ });
    

    {where: {property: value}} 
    

    你可以从中发现更多 Loopback Querying data Loopback Where filter postgresql RETURN QUERY

        4
  •  1
  •   IftekharDani    7 年前

    您可以尝试使用

     let searchField = "nomad";
     var search = new RegExp(searchField, 'i');
    {"where": {"id":search,"name" : search}
    

    我有用处 查找 .

     let searchField = "anyname";
         var searchCond = [];
             if (searchField) {
                            var search = new RegExp(searchField, 'i');
                            searchCond.push({ "id": search },{ "name": search });
                        }
    

        var modelCollection = Model.getDataSource().connector.collection(Model.modelName);
        modelCollection.aggregate([{
                            $match: {
                                $or: searchCond
                            }
                        }], function(err, data) {
        if (err) return callback(err);
        return callback(null, data);
      });