【问题标题】:Couchbase Java API and javascript view not returning value for a specific KeyCouchbase Java API 和 javascript 视图不返回特定键的值
【发布时间】:2013-07-26 19:17:35
【问题描述】:

我在 java 中使用 couchbase API

View view = client.getView("dev_1", "view1");
    Query query = new Query();
    query.setIncludeDocs(true);
    query.setKey(this.Key);
    ViewResponse res=client.query(view, query);
    for(ViewRow row: res)   
    {
          // Print out some infos about the document
        a=a+" "+row.getKey()+" : "+row.getValue()+"<br/>";

    }

    return a;

以及couchbase中的java脚本视图

function (doc,meta) {
  emit(meta.id,doc);
}

所以,当我删除语句 query.setkey(this.Key) 时,它会返回所有表,我在这里缺少什么.. 如何更改函数以仅反映键中提到的表名

【问题讨论】:

  • 1.什么是this.Key 变量,它在哪里定义以及它有什么值? 2. “桌子”是什么意思? Couchbase 没有任何桌子。 (举个例子)。 3.如果你使用setIncludeDocs(true);,你应该使用emit(meta.id,null);而不是emit(meta.id,doc);
  • this.Key 有表名,即我的 JSON 文档中有一个名为 table 的字段,所以我想查询 doc.table=this.key 获取结果集并返回它
  • 所以尝试发出(doc.table,null);

标签: javascript couchbase membase


【解决方案1】:

像这样改变地图功能:

      function (doc,meta) {
           emit(doc.table,null);
      }

最好不要发出整个文档,例如:

      emit(doc.table, doc)

注意:这非常重要:

我已经尝试在 Java 项目中多次使用 setKey("key") 并使用 CouchBase Console 3.0.1 的 Filter Result 对话框设置密钥,但没有返回任何内容。

有一天,我使用了setInclusiveEnd,它奏效了。我检查了 CouchBase Console 3.0.1 的 Filter Result 对话框中的 setInclusiveEnd 复选框,我得到了 json 输出。

    query.setKey("whatEverKey");
    query.setInclusiveEnd(true);

我希望这将有助于其他有同样问题的人。如果有人找到其他出路,请随时添加评论。

我不知道为什么他们的文档没有说明这一点。

额外

如果您的 json 是从 Java 项目中的实体类派生的,请确保包含 if statement 以测试实体类名称的 json 字段以包含您的 emit 语句。这将避免密钥作为null 发出:

      if(doc._class == "path.to.Entity") {
          emit(doc.table, null);
      }

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    相关资源
    最近更新 更多