CouchDb view - key in a list

有些话、适合烂在心里 提交于 2020-01-06 04:08:26

问题


I Want to query CouchDB and I have a specific need : my query should return the name field of documents corresponding to this condition : the id is equal or contained in a document filed (a list). For example, the field output is the following :

"output": [
       "doc_s100",
       "doc_s101",
       "doc_s102",
       "doc_s103",
   ],

I want to get all the documents having in their output field "doc_s102" for example. I wrote a view in a design document :

"backward_by_docid": {
           "map": "function(doc) {if(doc.output) emit(doc.output, doc.name)}"
       }

but this view works only when I have a unique value in the output field. How can I resolve this query ?

Thanks !


回答1:


you have to iterate over the array:

  if(doc.output) {
      for (var curOutput in doc.output) {
        emit (doc.output[curOutput],doc.name);
      }
  }

make sure that output always is an array (at least [])

.. and, of course use key="xx" instead key=["xxx"]



来源:https://stackoverflow.com/questions/9671509/couchdb-view-key-in-a-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!