Simplify Couchdb JSON response

前端 未结 1 1590
遥遥无期
遥遥无期 2021-02-01 23:37

I\'m storing location data in Couchdb, and am looking for a way to get an array of just the values, instead of key: value for every record. For example:

The current resp

相关标签:
1条回答
  • 2021-02-01 23:56

    You can use _show and _list functions, they take either a document or a view (respectively) and can send back a transformed response in whatever format you need. (in this case, JSON)

    Update: I ran a simple test with the data you provided here on my own CouchDB. Here's the list function I ended up writing. Customize it to fit your needs. :)

    function (head, req) {
        // specify that we're providing a JSON response
        provides('json', function() {
            // create an array for our result set
            var results = [];
    
            while (row = getRow()) {
                results.push({
                    city: row.value.city,
                    address: row.value.address
                });
            }
    
            // make sure to stringify the results :)
            send(JSON.stringify(results));
        });
    }
    
    0 讨论(0)
提交回复
热议问题