沙发床可以做循环吗 [英] can couchdb do loops

查看:63
本文介绍了沙发床可以做循环吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

couchdb可以循环吗?

Can couchdb do loops?

假设我有一个兴趣数据库,其中包含3个字段 subject1,subject2,subject3.例如猫,营养,头发或空间,望远镜,光学元件等.

Let's say I have a database of interests that have 3 fields subject1,subject2,subject3. example, cats,nutrition,hair or space,telescopes,optics etc.

一个人(A)有10个兴趣点,每个兴趣点都由3个字段组成.

A person (A) has 10 interests composed of 3 fields each.

另外10个人B,C,D ...拥有10个兴趣点,每个兴趣点都由3个主题组成.

10 more people B,C,D...have 10 interests each composed of 3 subjects each.

A人登录后,我希望系统搜索兴趣匹配的所有人.

When person A logs in I want the system to search for all people with matching interests.

在javascript中,我通常会遍历所有兴趣,然后找到我猜使用的匹配兴趣 两个循环.然后将匹配项存储在该用户的另一个数据库中,例如"matchinginterests".

In javascript I would normally loop through all the interests and then find matching ones I guess using two loops. Then store the matches in another database for the user like "matchinginterests".

在长沙发上,与mysql相比,有什么简单的方法可以做到这一点-看起来很复杂.

Is there any easy way to do this in couchdb compared to mysql -- which seems very complicated.

谢谢, 丹

推荐答案

我想我理解您的要求.使用Map/Reduce答案很简单.

I think I understand what you are asking. The answer is pretty straightforward with Map/Reduce.

假设您有以下人员文档:

Say you have the following people documents:

{
   "name": "Person A",
   "interests" [ "computers", "fishing", "sports" ]
}
{
   "name": "Person B",
   "interests" [ "computers", "gaming" ]
}
{
   "name": "Person C",
   "interests" [ "hiking", "sports" ]
}
{
   "name": "Person D",
   "interests" [ "gaming" ]
}

您可能希望将您的密钥作为利息发出,并将值作为该人的姓名(或_id).

You would probably want to emit your key as the interest, with the value as the person's name (or _id).

function (doc) {
   for (var x = 0, len = doc.interests.length; x < len; x++) {
      emit(doc.interests[x], doc..name);
   }
}

您的查看结果如下:

  • 计算机=>人员A
  • 计算机=>人员B
  • 钓鱼=> A人
  • 游戏=> B人
  • 游戏=>人D
  • 远足=> C人
  • 体育=>人物A
  • 体育=> C人

要获取感兴趣的计算机用户列表,只需将key="computers"作为查询字符串的一部分发送即可.

To get a list of people with computers as an interest, you can simply send key="computers" as part of the query string.

如果要在地图上添加化简功能,只需使用_count(使用已编译化简功能的快捷方式),并且可以检索所有具有特定兴趣的人员的数量,甚至可以使用以限制您查询建立关系的兴趣.

If you want to add a reduce function to your map, you can simply use _count (shortcut to use a compiled reduce function) and you can retrieve a count of all the people with a particular interest, you can even use that to limit which interests you query to build your relationships.

这篇关于沙发床可以做循环吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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