如何仅根据 rethinkdb 中的过滤器检索特定字段

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

我正在使用 rethinkdb 和 koajs 构建数据库。无论如何,Reql 是否只提供过滤功能中的特定字段?这是我的代码:

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() 应该只 return 该字段,但所有数据仍会发送到浏览器。

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);