couchdb加入了2个文档 [英] couchdb joins for 2 documents

查看:74
本文介绍了couchdb加入了2个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ouchdb的新手,我需要进行加入并从2个文档类型中获取结果.

I am new to couchdb and I need to make a join and get results from 2 documents type.

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

I have type="registeredEvent" in one document and type="user" in another. The schema of both the type are as follows:

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"
}

我想获取用于填充图形的数据.

I want to get data for populating a graph.

  1. 我有一个eventid列表,我想获得这些事件中的女性人数.
  2. 我也希望每个事件中都没有用户,但是年龄在17-28岁之间

我正在使用以下内容来获取数据

I am using the following to fetch data

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

有人可以帮助我吗?我尝试了很多事情,但没有成功.我无法检查自己的走法是否正确,因为所有事情都为我提供了错误的数据.

Can anybody help me out. I have tried a number of things but of no success. I could not check if I am on the right track as all the things provided me with wrong data.

推荐答案

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

You can get data of gender, age using list like:

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}))
 }

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

You can extend this to support more age groups if needed...

这篇关于couchdb加入了2个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆