couchdb 加入 2 个文档

couchdb joins for 2 documents

我是 couchdb 的新手,我需要进行连接并从 2 种文档类型中获取结果。

我在一个文档中有 type="registeredEvent",在另一个文档中有 type="user"。两种类型的架构如下:

registeredEvent

{
   "_id": "316ff4844ce56...",
   "_rev": "2-5dcfb85eaea3f....",
   "eventid": "0620F01F-3B6B-DE07-860A-SF453FF5AELP11",
   "email": "amaya@gmail.com",
   "type": "registeredEvent"
}

用户

{
   "_id": "Profile_amaya@gmail.com",
   "_rev": "1-89768208cfb5650ee....",
   "name": "amaya",
   "email": "amaya@gmail.com",
   "dob": "91-07-14",
   "gender": "Female",
   "location": "canada",
   "facebook": "dghdfj",
   "twitter": "fgfghh",
   "type": "user"
}

我想获取填充图表的数据。

  1. 我有一个 eventid 的列表,我想计算这些事件中的女性人数。
  2. 我还想知道每个事件中的用户数,但年龄段在 17-28 岁之间

我正在使用以下方法获取数据

db.query('event/getGraphdata1...',{keys: [<array of eventid's>], include_docs: true })

谁能帮帮我。我尝试了很多事情但没有成功。我无法检查我是否在正确的轨道上,因为所有的东西都为我提供了错误的数据。

您可以使用以下视图从这 2 个文档中获取一个结果集:

function(doc) {
  if (doc.type === 'registeredEvent') {
     emit(doc.eventid, {_id:"Profile_" + doc.email}, doc.user);
   }
}

使用 HTTP API 请求时添加 include_docs=true

您可以使用如下列表获取性别、年龄数据:

function(head, req) { 
       var row; var counts = {}; var male = female = age18 = 0;
       while(row = getRow()) {
         if(row.doc != null) { 
            if(row.doc.gender != null && row.doc.gender.toLowerCase() === 'male') 
                male++; 
            else 
                female++;
            if(row.doc.dob != null && Array.isArray(row.doc.dob)) { 
                var age = 0;
                var date1 = new Date(row.doc.dob[0], row.doc.dob[1], row.doc.dob[2]);
                var date2 = new Date();  var m = date1.getMonth(); 
                var years = date2.getFullYear() - date1.getFullYear(); 
                date1.setFullYear(date1.getFullYear() + years); 
                if (date1.getMonth() != m) date1.setDate(0); 
                age = date1 > date2 ? --years : years; 

                if(age < 29) 
                    age18++;
            }
         }
     }
     counts['male'] = male;counts['female'] = female; counts['age18'] = age18; send(JSON.stringify({'counts' : counts}))
 }

如果需要,您可以扩展它以支持更多年龄段...