开发者

Why do CouchDB view rows use "key": key, "value":value & not simply key:value

开发者 https://www.devze.com 2023-01-10 13:43 出处:网络
Why does curl http://localhost:5984/blog/_design/comments/_view/total_num?group=true return {\"rows\":[

Why does curl http://localhost:5984/blog/_design/comments/_view/total_num?group=true return

{"rows":[
  {"key":"sum","value":23},
]}

and not

{"rows":[
  {"sum": 2开发者_高级运维3},
]}


There are a couple different reasons.

  • As Tim McNamara points out, having the key as the member name in the result row means that keys are limited to strings because of the rules of JSON. This way allows people to have view keys of any JSON type.

  • As Alex Koshelev points out, if we allowed keys as object member names in the view row then the key and value would not be directly addressable. This means that you would have to investigate each and every row to figure out what the key was.

  • A second aspect of the namespace issue is that a key could conflict with any metadata that may be included in that row. For instance with include_docs=true or the included docid member for non-reduced view output.

Alternatively, if you would like to reformat the output to suit your needs, you can use a _list function to change each row to your liking.


In addition to Alex and Tim's responses:

  • The view's keys may not be unique, i.e. the same key may have been emitted for multiple documents or even multiple times for a single document.
  • The view's rows are ordered by key. JSON's object type is an "unordered set of name/value pairs". Many languages, including JavaScript, do not define the order of keys in a mapping. A list is therefore a better representation for something with order.


Allows for null objects as keys.


Each row can have additional data, such as document data (doc) for include_docs=true queries.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号