是否可以在YDN-DB中找到多个值(例如sql IN关键字)?

    //simple scenario
    "SELECT * FROM categories WHERE code IN ('AB','CD','EF','GH','IJ','KL');"

    var code = "'AB','CD','EF','GH','IJ','KL'";
    var key_range = ydn.db.KeyRange.only(code);
    cacheDb.values('categories','code',key_range).then(function(record){
        console.log(record);
    },function(e){
        console.log(e);
    });

最佳答案

您可以使用多查询方法,如最近更新的tutorial中所述。

基本上,您将为IN子句的每个元素创建迭代器,并将结果联接(联合),如下所示:

var keys = [];
var multiQuery = function(sec_keys, pri_keys) {
  var advance = [];
  var cmp = ydn.db.cmp(pri_keys[0], pri_keys[1]);
  if (cmp == 1) { // pri_keys[0] > pri_keys[1]
    if (keys[keys.length - 1] != pri_keys[1]) {
      keys.push(pri_keys[1]);
    }
    advance[1] = true; // advance iter_1 on step
  } else if (cmp == -1) { // pri_keys[0] < pri_keys[1]
    if (keys[keys.length - 1] != pri_keys[0]) {
      keys.push(pri_keys[0]);
    }
    advance[1] = true; // advance iter_1 on step
  } else { // pri_keys[0] == pri_keys[1]
    if (keys[keys.length - 1] != pri_keys[0]) {
      keys.push(pri_keys[0]);
    }
    advance[0] = true; // advance iter_0 on step
    advance[1] = true; // advance iter_1 on step
  }

  if (keys.length >= 10) {
    return [];
  } else {
    return advance;
  }
};


然后使用scan方法:

db.scan(multiQuery, [iterators...]).done(function() {
  db.values('article', keys).done(function(values) {
    console.log(keys, values);
  })
});

关于javascript - 在YDN-DB中搜索多个值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27497768/

10-11 13:55