tags:

views:

105

answers:

4

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

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

and not

{"rows":[
  {"sum": 23},
]}
A: 

Allows for null objects as keys.

Tim McNamara
A: 

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

Alex Koshelev
A: 

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.
Matt Goodall
+3  A: 

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.

Paul J. Davis