How to get from CouchDB only certain fields of certain documents with a single request?

后端 未结 1 1438
太阳男子
太阳男子 2021-01-22 16:37

create a view that return only a subset of values from a document, each with its key and value within a json string. like if one given view returns a document as this following,

1条回答
  •  梦毁少年i
    2021-01-22 16:58

    A map function that emits a new document with selected fields only. As an example, let's map field1 (a string) and field9 (an array) only:

    function map(doc) {
      emit(doc._id, {
        field1: doc.field1, 
        field9: doc.field9
      });
    }
    

    In the above example, each document will be fired with a key being the original doc ID and the value being the mapped fields you require. This is useful if you are planning to add a reduce function later.

    Depending on your use case, you may just want to emit the mapped objects:

    function map(doc) {
      emit({
        field1: doc.field1, 
        field9: doc.field9
      });
    }
    

    Please see http://guide.couchdb.org/draft/views.html The documentation on building data views is pretty good, you can discover a lot by experimenting..

    0 讨论(0)
提交回复
热议问题