title: CouchDB Map syntax
body: <p>I have documents in CouchDB (v. 2.1.1) as follows:</p>
  
  <pre><code>{
    "xyz": "a",
    "abc": "def"
  },
  {
    "xyz": "a",
    "ghi": "jkl"
  },
  {
    "xyz": "a",
    "mno": "pqr"
  },
  {
    "xyz": "a",
    "stu": "vwx"
  },
  {
    "xyz": "a",
    "bcd": 1000
  }
  </code></pre>
  
  <p>If I run a simple map function, for example:</p>
  
  <pre><code>function (doc) {
  if (doc.xyz ){
  emit(doc.xyz, doc.abc);}}
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>{
    "id": "4c3406a1d92942b4fb10d1314e0061a9",
    "key": "a",
    "value": "def"
  },
  {
    "id": "4c3406a1d92942b4fb10d1314e006ccf",
    "key": "a",
    "value": null
  },
  {
    "id": "4c3406a1d92942b4fb10d1314e00787f",
    "key": "a",
    "value": null
  },
  {
    "id": "4c3406a1d92942b4fb10d1314e00871e",
    "key": "a",
    "value": null
  },
  {
    "id": "4c3406a1d92942b4fb10d1314e00906a",
    "key": "a",
    "value": null
  }
  </code></pre>
  
  <p>I want to try and eliminate the 'null' outputs.</p>
  
  <p>I am looking at having a CouchDB database with many small documents containing small snippets of information rather than having larger documents containing much more information per document.</p>
  
  <p>My question is, is my document design a good one and if so how do I get just what I am looking for rather than rows of 'nulls'. If my storage design is not ideal, what kind of design should I be looking at to simplify the output given my plan to have many small 'docs'.</p>
  
  <p>EDIT:</p>
  
  <p>Having looked at possible answers, I have decided that having numerous small documents as I described in my question is not giving me the kind of benefit I imangined they would.</p>
  
  <p>I was unable to get a satisfactory solution to the map function to get readable answers.</p>
  
  <p>However, I investigated the 'Mango' query system available in recent updates of CouchDB and I was able using these queries to get acceptable output from a database like my supplied one.</p>
  
  <p>This is what I did:</p>
  
  <pre><code>curl -X POST http://admin:123@127.0.0.1:5984/ptn/_find -d '{"selector":   {"$or": [{"abc": {"$gt": null}},{"ghi": {"$gt": null}}]},"fields": ["abc","ghi"]}' -H "Content-Type:application/json"
  </code></pre>
  
  <p>Un-minified:</p>
  
  <pre><code>{
    "selector": {
      "$or": [
        {
          "abc": {
            "$gt": null
          }
        },
        {
          "ghi": {
            "$gt": null
          }
        }
      ]
    },
    "fields": [
      "abc",
      "ghi"
    ]
  }
  </code></pre>
  
  <p>The output:</p>
  
  <pre><code>{"docs":[
  {"abc":"def"},
  {"ghi":"jkl"}
  ]
  .....
  </code></pre>
  
  <p>A concise answer.</p>
  
  <p>Sorting can be done but sorted fields must be indexed. Indexing is in any case advised for larger data sets.</p>
  
  <p>Reference:</p>
  
  <p><a href="http://docs.couchdb.org/en/2.1.1/api/database/find.html" rel="nofollow noreferrer">http://docs.couchdb.org/en/2.1.1/api/database/find.html</a></p>
  
  <p>As my question required a map function, this perhaps cannot be regarded as a valid answer but for me it is an answer. I have tried the 'Mango' query system a little on other databases and it seems to be more useful/powerful than I thought is was although it offers no means of totaling etc.</p>