1

我是 CouchBase 的新手。我有几个简单的文件:

1. {
  "docType": "DeviceData",
  "id": "57 0F 75 00 C8 0B@2013-06-26 23:59:38.979",
  "time": 1372271378979,
  "group": "London"
}

2. {
  "docType": "DeviceData",
  "id": "57 0F 75 00 C8 0B@2013-06-27 10:02:46.197",
  "time": 1372307566197,
  "group": "Bristol"
}

3. {
  "docType": "DeviceData",
  "id": "57 0F 75 00 C8 0B@2013-06-27 10:03:36.4",
  "time": 1372307616400,
  "group": "Bristol"
}

我有要求查询组和时间。例如,我想获取 group = Bristol 的所有文档,时间从 1372307616100 到 1372307616500。所以我处理我应该得到 3 个文档中的 2 个。

所以我将视图创建为:

function (doc, meta) {
  if(doc.docType == "DeviceData")
  emit([doc.group, doc.time], doc);
}

在java代码中设置查询如下:

String str = "[\"Bristol\", 1372307566100]";
        String end = "[\"Bristol\", 1372307616500]";
        query.setRange(str, end);
        List<DeviceData> deviceData = cbStore.getView("getDevices", DeviceData.class, query);

但获得零文件。

请让我知道我做错了什么吗?需要帮助谢谢。

*编辑: 我尝试使用复杂键,如下所示,但没有运气。

ComplexKey startKey = ComplexKey.of("Bristol", "1372307566100");
ComplexKey endKey = ComplexKey.of("Bristol", "1372307616500");
4

1 回答 1

5

问题是在您的对象中时间很长,而不是字符串:

query.setStale(Stale.FALSE);
long firstParameter=1372307566197l;
long secondParameter=1372307616400l;
//["Bristol",1372307566197]
ComplexKey startKey = ComplexKey.of("Bristol", firstParameter);
//["Bristol",1372307616400]
ComplexKey endKey = ComplexKey.of("Bristol",  secondParameter);
query.setRange(startKey, endKey);
ViewResponse result = client.query(view, query);
Iterator<ViewRow> iter = result.iterator();
while(iter.hasNext()) {
    ViewRow row = iter.next();      
    System.out.println( row.getId()); // ID of the document
    System.out.println(row.getKey()); // Key of the view row
    System.out.println(row.getValue()); // Value of the view row
    System.out.println(row.getDocument()); // Full document if included
    }
于 2013-10-21T09:38:11.080 回答