how do I create a “like” filter view in couchdb

前端 未结 7 835
北海茫月
北海茫月 2020-12-30 03:25

Here\'s an example of what I need in sql:

SELECT name FROM employ WHERE name LIKE %bro%

How do I create view li

7条回答
  •  我在风中等你
    2020-12-30 04:07

    I know it is an old question, but: What about using a "list" function? You can have all your normal views, andthen add a "list" function to the design document to process the view's results:

    {
      "_id": "_design/...",
      "views": {
        "employees": "..."
      },
      "lists": {
        "by_name": "..."
      }
    }

    And the function attached to "by_name" function, should be something like:

    function (head, req) {
      provides('json', function() {
        var filtered = [];
    
        while (row = getRow()) {
          // We can retrive all row information from the view
          var key = row.key;
          var value = row.value;
          var doc = req.query.include_docs ? row.doc : {};
    
          if (value.name.indexOf(req.query.name) == 0) {
            if (req.query.include_docs) {
              filtered.push({ key: key, value: value, doc: doc});
            } else {
              filtered.push({ key: key, value: value});
            }
          }
        }
    
        return toJSON({ total_rows: filtered.length, rows: filtered });
      });
    }

    You can, of course, use regular expressions too. It's not a perfect solution, but it works to me.

提交回复
热议问题