how to only retrieve a specific field based on a filter in rethinkdb

时间:2015-07-28 16:12:09

标签: rethinkdb koa

I'm building a database with rethinkdb and koajs. Is there anyway in Reql to only deliver a specific field within a filter function? here is my code:

function bySwimmer() {
    return function *(next) {

      var qs = this.request.querystring;
      var q = this.request.query;

      //pulls data from the db
      var cursor = yield r.table('swim').filter(function(swim) {

        //if there is no query it will return all data else returns only the data specified by the query
        if(qs == "") {
          return swim.getField("fullName");
        } else {
          return swim('fullName').upcase().eq(q.fullname.toUpperCase());
        }

      }).run(this._rdbConn);
      var result = yield cursor.toArray();
      this.body = JSON.stringify(result);

      yield next
    }
}

getField() should only return that field but all the data is still sent to the browser.

1 个答案:

答案 0 :(得分:1)

filter命令仅确定返回的行,不允许您更改它们。您可以.map或仅()修改返回的行。例如:

var query = r.table('swim');

if(qs == "") {
  query = query('fullName');
} else {
  query = query.filter(function(swim) {
    swim('fullName').upcase().eq(q.fullname.toUpperCase())
  });
}

var cursor = yield query.run(this._rdbConn);