view的做用是从没有结构和半结构的数据对象中抽取过滤须要的信息,并生成相关的index信息,一般生成json数据。 view经过迭代bucket全部文档而提取过滤信息,进而生成index。node
一个bucket能够有多个设计文档,一个设计文档(Design Document)能够有多个views。git
开发view和产品view开发view 以dev——前缀开始,只对部分数据作index和view,以帮助不用对全部的数据作view, 而调试获得正确的view,以后能够切换到产品模式(去掉dev_前缀)github
view名必定要是一个或多个uft编码字符,首尾中间不含空白(空格, tab等)数据库
索引和查询存储的对象json
构建输出指定的对象类型数组
从数据库抽取过滤信息缓存
计算,统计,汇集数据集合的信息app
view的输出对应每一个emit有三个重要属性函数
Document IDthis
每次调用emit()都会包含 document id,帮助加载全部的文档经过get()
View key
这是emit()的第一个参数,你能够指定key任何值,包括数组(一组值)来查询复杂的选择和报告。 key的值也决定如何插叙
View value
这是emit()的第二个参数,只能被自定义的reduce方法使用。
Speific Key(指定的键)
$result = $cb->view("recipes", "bytitle", array('key' => 'Apple Pie'));
One or more keys
$result = $cb->view("dev_recipes", "bytitle", array('keys' => array('Shepherds pie', 'Mariners pie')));
Key range
#会找出tittle在Meat loaf到Mexican tacos间全部的数据 $result = $cb->view("dev_recipes", "bytitle", array('startkey' => 'Meat loaf','endkey' => 'Mexican tacos'));
key能够帮助过滤查询,但view也有排序和其余需求:
descending
Return the documents in descending by key order
endkey
Stop returning records when the specified key is reached. Key must be specified as a JSON value.
endkey_docid
Stop returning records when the specified document ID is reached full_set Use the full cluster data set (development views only).
group
Group the results using the reduce function to a group or single row
group_level
Specify the group level to be used
inclusive_end
Specifies whether the specified end key should be included in the result
key
Return only documents that match the specified key. Key must be specified as a JSON value.
keys
Return only documents that match each of keys specified within the given array. Key must be specified as a JSON value. Sorting is not applied when using this option.
limit
Limit the number of the returned documents to the specified number
on_error
Sets the response in the event of an error. stop will stop returning rows; continue will notify you of the error, but continue returning rows from other nodes.
reduce
Use the reduction function.
skip
Skip this number of records before starting to return the results
stale
Allow the results from a stale view to be used. ok uses a stale index; false forces an index update; up date_after updates the index after it has been accessed (default)
startkey
Return records with a value equal to or greater than the specified key. Key must be specified as a JSON value.
startkey_docid
Return records starting with the specified document ID
有时可能由于应用的版本,而形成输出不一样等,而输入的数据格式也不一样,emit 能够出现屡次,经过选择控制输出不一样格式(JS):
function (doc, meta){ if (doc.preptime && doc.cooktime){ emit(parseInt(doc.preptime, 10) + parseInt(doc.cooktime, 10), null); } else{ emit(parseInt(doc.totalcooktime, 10), null); } }
当须要使用到reduce时,这时emit的第二个参数,就须要传入value,若是不要用到,就像上面例子传入null就好
index更新会在各个节点同步时,触发更新。Couchbase在读写时(get, set)时,先从缓存层开始,只用序列化更新到磁盘,才会更新index:
index会根据服务配置设定的更新频率自动更新。
在query时,能够指定是否更新index
删除文档时,只用硬盘上的数据被删除,index才会被删除
文档有TTL过时时限,相关index会自动更新当文档过时
在客户端获取数据时,设置 -stale- 参数能够设定三种index 更新状态:
update_after
在获取数据后更新index,也就是下次查询时使用的是更新的view
ok
使用当前版本的index,不触发index更新
false
强制更新全部的索引后返回查询结果,也许会很费时间,由于要更新全部的view和index。
_count内建函数用来统计来自map的输入行
_sum 会把map输出的值或多个值加起来。
用于汇集统计,最大,最小等值。
meta 含以下信息和字段:
id
数据对象的ID或键(key),和set方法用来写数据的key是同样的
rev
内建版本号,用来追踪当前数据的版本。rev字段含的信息是不具备一致性或可跟踪性,不可用于客户端应用中。
type
保存文档的类型, JSON文档是json类型, 二进制文档是base64类型
flags
flags是一个32为的整数,用来保存数据保存建立的时间, 可用状态由客户端是否支持决定
expiration
数据对象过时时间 ,和TTL的表示一致