您将如何在此文档结构上使用map reduce? [英] How would you use map reduce on this document structure?

查看:87
本文介绍了您将如何在此文档结构上使用map reduce?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想计算foobar.relationships.friend.count,我将如何针对此文档结构使用map/reduce以使计数等于22.

If I wanted to count foobar.relationships.friend.count, how would I use map/reduce against this document structure so the count will equal 22.

[
    [0] {
              "rank" => nil,
        "profile_id" => 3,
          "20130913" => {
            "foobar" => {
                    "relationships" => {
                      "acquaintance" => {
                        "count" => 0
                    },
                    "friend" => {
                          "males_count" => 0,
                                  "ids" => [],
                        "females_count" => 0,
                                "count" => 10
                    }
                }
            }
        },
          "20130912" => {
            "foobar" => {
                    "relationships" => {
                      "acquaintance" => {
                        "count" => 0
                    },
                    "friend" => {
                          "males_count" => 0,
                                  "ids" => [
                            [0] 77,
                            [1] 78,
                            [2] 79
                        ],
                        "females_count" => 0,
                                "count" => 12
                    }
                }
            }
        }
    }
]

推荐答案

在JavaScript中,此查询可为您提供预期的结果

In JavaScript this query get you the result you expect

r.db('test').table('test').get(3).do( function(doc) {
  return doc.keys().map(function(key) {
    return r.branch(
      doc(key).typeOf().eq('OBJECT'),
      doc(key)("foobar")("relationships")("friend")("count").default(0),
      0
    )
  }).reduce( function(left, right) {
    return left.add(right)
  })
})

在Ruby中,应该是

r.db('test').table('test').get(3).do{ |doc|
  doc.keys().map{ |key| 
    r.branch(
      doc.get_field(key).typeOf().eq('OBJECT'),
      doc.get_field(key)["foobar"]["relationships"]["friend"]["count"].default(0),
      0
    )
  }.reduce{ |left, right|
    left+right
  }
}

我还倾向于认为您使用的架构并未真正适应,最好使用类似的内容

I would also tend to think that the schema you use is not really adapted, it would be better to use something like

{
  rank: null
  profile_id: 3
  people: [
    {
      id: 20130913,
      foobar: { ... }
    },
    {
      id: 20130912,
      foobar: { ... }
    }
  ]
}

一种不使用r.branch的简单方法就是使用without命令删除不是对象的字段.

A simpler way to do it without using r.branch is just to remove the fields that are not objects with the without command.

例如:

r.db('test').table('test').get(3).without('rank', 'profile_id').do{ |doc|
  doc.keys().map{ |key| 
    doc.get_field(key)["foobar"]["relationships"]["friend"]["count"].default(0)
  }.reduce{ |left, right|
    left+right
  }
}.run

这篇关于您将如何在此文档结构上使用map reduce?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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