如何使用CouchRest模型访问CouchDB文档

时间:2011-12-21 10:55:58

标签: ruby-on-rails json model couchdb couchrest

请帮我解决这个问题。我的CouchDB中有大约1500个文档,每个文档属于任何一种类型('调查'和'响应')。

我需要以下解决方案

1]需要在文档类型(documentType)为“Survey”的下拉列表中显示Document Ids(_id)

2]如果我从下拉列表中选择一个项目(_id),我需要在列表框中显示属于所选ID的所有文档ID(_id)(从下拉列表中选择_id)

我总共有1500个这样的文件

{
   "_id": "ff2fb2554682ba613c2f83c63502808a",
   "_rev": "4-dd559696434a402739e789ccc8c9a481",
   "answers": [
       {
           "answers": [
               "Aug 19,2011"
           ],
           "questionId": "50f3df434ae02ac7fff48c1c2bde83e4"
       },
       {
           "answers": [
               "Bechu Yadav"
           ],
           "questionId": "15504f006e58872fd94871fe0c9d32ad"
       },
       {
           "answers": [
               "35"
           ],
           "questionId": "3c447a2f5cc6ca1985ce2c81463a3c47"
       },
       {
           "answers": [
               "Male"
           ],
           "questionId": "a6df7c25602939554612ef6de762f5b9"
       },
       {
           "questionId": "2f66155965e60094f23f01af531d5af1",
           "subAnswers": [
               {
                   "questionId": "1dae2d86eef846c967254c9e369170ce"
               },
               {
                   "questionId": "40ffeb4d33dab1fe8d2d8b73c02ed13b"
               },
               {
                   "questionId": "2db1e8b6e97d5baa8935b9b31fcc9648"
               },
               {
                   "questionId": "0f8bb9f91ea8085b4ffb839ee8deabb5"
               },
               {
                   "questionId": "22a93ecb72c50ff8899f8b2937776e51"
               },
               {
                   "questionId": "e5106384790c2be745c952c4b867a0ff"
               }
           ]

           "questionId": "492cf9bd41257ea478c5222fbba06616"
       }
   ],
   "createdAt": "2011-08-19T21:05:36.486+0000",
   "createdBy": "user4",
   "documentType": "Response",
   "ipAddress": "42.110.85.67",
    "location": {
       "latitude": 26.8415613,
       "longitude": 75.8222883,
       "provider": "network"
   },
    "surveyId": "6df022f0f371752167ad4920b38e1c37",
    "published": true
}

我的CouchRest模型如下所示

class Android < CouchRest::Model::Base
    property :description, String
    property :_id, String
    property :_rev, String
    property :documentType, String

    design do
      view :by_documentType,:map =>"function(doc){if (doc.documentType == 'Survey') {emit(doc._id);}}"
      view :by_createdBy
    end
 end

但是当我访问 by_documentType 时,它返回 nil

Android.find_by_documentType('Survey')

2 个答案:

答案 0 :(得分:1)

谢谢朋友们!

最后我找到了答案,我改变了我的模型如下

require 'couchrest_model'

class Android < CouchRest::Model::Base

  property :description, String
  property :_id, String
  property :_rev, String
  property :documentType, String
  property :surveyId, String

  design do
    view :by_document_type,:map =>"function(doc) {if (doc.documentType == \"Survey\") {emit([doc._id,doc.title], 1);}}",:reduce=>"function(keys, values, rereduce) {return sum(values);}"
    view :by_surveyId,:map =>"function(doc) {emit([doc.surveyId], 1);}",:reduce=>"function(keys, values, rereduce) {return sum(values);}"
  end
end

现在可以根据需要调用此视图。以下内容返回所有文件类型为“调查”的文件

Android.by_document_type.rows

根据调查ID选择,我可以检索属于所选调查ID的所有文件,如下所示

Android.by_surveyId.key('Selected Survey ID goes here').rows

答案 1 :(得分:0)

尝试使用Android.by_documentType.key('调查')