Couchbase中的复合视图 [英] Composite views in couchbase

查看:47
本文介绍了Couchbase中的复合视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Couchbase的新手,正在努力获取复合索引来执行我想要的操作.用例是这样的:

I'm new to Couchbase and am struggling to get a composite index to do what I want it to. The use-case is this:

  • 我有一组枚举"存储为文档
  • 每个域都有一个"last_updated"字段,正如您可能已经猜到的那样,该字段存储该字段的上次更新时间
  • 我只希望显示那些自给定日期以来已更新的枚举,但仍按枚举名称对列表进行排序

我已经创建了一个这样的Couchbase视图:

I've created a Couchbase View like this:

function (doc, meta) {
   var time_array;
   if (doc.doc_type === "enum") {
      if (doc.last_updated) {
         time_array = doc.last_updated.split(/[- :]/);
      } else {
         time_array = [0,0,0,0,0,0];
   }
   for(var i=0; i<time_array.length; i++) { time_array[i] = parseInt(time_array[i], 10); }
   time_array.unshift(meta.id);
   emit(time_array, null);
}

}

我有一条记录没有设置last_updated字段,因此它的时间字段都设置为零.我认为,作为第一个测试,我可以过滤掉该结果,然后输入以下内容:

I have one record that doesn't have the last_updated field set and therefore has it's time fields are all set to zero. I thought as a first test I could filter out that result and I put in the following:

startkey = ["a",2012,0,0,0,0,0]
endkey = ["Z",2014,0,0,0,0,0]

虽然列表按"id"排序,但它没有过滤任何内容!谁能告诉我我做错了什么?是否有更好的综合视图来实现这些结果?

While the list is sorted by the 'id' it isn't filtering anything! Can anyone tell me what I'm doing wrong? Is there a better composite view to achieve these results?

推荐答案

在沙发床中,当您通过startkey-endkey查询视图时,无法按2个或更多属性过滤结果.Couchbase只有一个索引,因此它将仅按第一个参数过滤结果.因此,您的查询将与以下查询相同:

In couchbase when you query view by startkey - endkey you're unable to filter results by 2 or more properties. Couchbase has only one index, so it will filter your results only by first param. So your query will be identical to query with:

startkey = ["a"]
endkey = ["Z"]

这是一个链接来完成Filipe Manana的答案,为什么不能用这些日期过滤它.

Here is a link to complete answer by Filipe Manana why it can't be filtered by those dates.

这是它的引文:

对于复合键(数组),元素从左到右进行比较,并且当一个元素与另一个键中的对应元素不同时,比较会立即结束(与比较字符串àla memcmp()或strcmp时发生的情况相同)()).

For composite keys (arrays), elements are compared from left to right and comparison finishes as soon as a element is different from the corresponding element in the other key (same as what happens when comparing strings à la memcmp() or strcmp()).

因此,如果要使用按日期过滤的视图,则日期数组应在复合键中排在首位.

So if you want to have a view that filters by date, date array should go first in composite key.

这篇关于Couchbase中的复合视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆