在couchbase中有多个可选键

时间:2013-03-25 19:19:44

标签: couchbase

我的地图功能

function(doc, meta) {
  emit([doc.city, doc.region], doc.id);
}

我的查询

?keys=[["New York", "Northeast"]]

这会发出:

{ 
  city: 'New York',
  region: Northeast
}

如果我想使用相同的地图功能只获取东北部的那些城市,它将无效。有没有办法只匹配其中一个键?像...

?keys=[[null, "Northeast"]]

并输出

[{ 
  city: 'New York',
  region: 'Northeast'
},
{
  city: 'Boston',
  region: 'Northeast'
}]

1 个答案:

答案 0 :(得分:6)

当你在Couchbase中使用查询/视图时,你只能从左到右阅读密钥。

因此,如果您希望能够查询每个“部分”,则需要创建一个发出的新视图(如果您想在同一视图中执行2发射或创建2个视图,则可以这样做)

emit(doc.region, doc.city);

并使用

查询它
?startkey=["Northeast"]&endkey["Northeast",{}]

另外,正如您所看到的,我没有发出doc.id,这不是必需的,因为doc.id总是由Couchbase视图发出。

相关问题