Why does CouchDB use โ€œkeyโ€: key, key, value: value, not just key: value

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

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

but not

 {"rows":[ {"sum": 23}, ]} 
+4
source share
4 answers

There are several different reasons.

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

  • As Alex Koshelev points out, if we resolved the keys as the names of the member objects in the view string, then the key and value will not be directly addressed. This means that you will need to examine each line to find out what the key is.

  • The second aspect of the namespace problem is that the key may conflict with any metadata that may be included on this line. For example, with include_docs = true or the included docid member for output without restrictions.

Alternatively, if you want to reformat the output to suit your needs, you can use the _list function to change each line to your liking.

+4
source

In addition to the answers of Alex and Tim:

  • View keys may not be unique, i.e. the same key can be issued for several documents or even several times for one document.
  • Presentation lines are sorted by key. The JSON object type is an "unordered set of name / value pairs." Many languages, including JavaScript, do not determine the order of keys in a mapping. Therefore, a list is the best view for something with order.
+1
source

Allows null objects as keys.

0
source

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

0
source

All Articles