【问题标题】:Rethinkdb: Calculate tag occurrence per userRethinkdb:计算每个用户的标签出现次数
【发布时间】:2015-05-22 15:46:25
【问题描述】:

我的表格包含如下所示的文档:

[{ user: { 
    key: '100' 
  },
  product: {
    name: 'Product 1',
    tags: [ 'tag1', 'tag2' ],
  }
}, { user: { 
    key: '100' 
  },
  product: {
    name: 'Product 1',
    tags: [ 'tag1', 'tag3' ],
  }
}, ...]

我想创建一个查询

  • user.key 字段对文档进行分组(结果中每个用户 1 个文档),
  • product.tags 将是一个对象(而不是数组),其中包含每个标签的标签出现次数。

结果示例:

[ { user: { 
    key: '100' 
  },
  product: {
    name: 'Product 1',
    tags: {
      tag1: 2, // tag1 found 2x for user.key=100
      tag2: 1, // tag2 found 1x for user.key=100
      tag3: 1
    }
  }
}, ...]

我想我可以通过映射和归约来做到这一点,但我遇到了问题 - 我是第一次使用 rethinkdb。

【问题讨论】:

    标签: rethinkdb


    【解决方案1】:

    这是一种方法:

    // Group by user key
    r.table('30400911').group(r.row('user')('key'))
      // Only get the product info inside the reduction
      .map(r.row('product'))
      .ungroup()
      .map(function (row) {
        return {
          user: row('group'),
          // Group by name
          products: row('reduction').group('name').ungroup().map(function (row) {
            return {
              name: row('group'),
              // Convert array of tags into key value pairs
              tags: r.object(r.args(row('reduction').concatMap(function (row) {
                return row('tags')
              }).group(function (row) {
               return row; 
              }).count().ungroup().concatMap(function (row) {
                return [row('group'), row('reduction')]
              })))
            }
          })
        }
      })
    

    对于以下数据:

    {
      "id":  "0565e91a-01ca-4ba3-b4d5-1043c918c79d" ,
      "product": {
        "name":  "Product 2" ,
        "tags": [
          "tag1" ,
          "tag3"
        ]
      } ,
      "user": {
        "key":  "100"
      }
    } {
      "id":  "39999c9f-bbef-4cb7-9311-2516ca8f9ba1" ,
      "product": {
      "name":  "Product 1" ,
        "tags": [
          "tag1" ,
          "tag3"
        ]
      } ,
      "user": {
        "key":  "100"
      }
    } {
      "id":  "566f3b79-01bf-4c29-8a9c-fd472431eeb6" ,
      "product": {
        "name":  "Product 1" ,
        "tags": [
          "tag1" ,
          "tag2"
        ]
      } ,
      "user": {
       "key":  "100"
      }
    } {
      "id":  "8e95c467-cedc-4734-ad4d-a1f7a371efd5" ,
      "product": {
        "name":  "Product 1" ,
        "tags": [
          "tag1" ,
          "tag2"
        ]
      } ,
      "user": {
        "key":  "200"
      }
    }
    

    结果是:

    [
      {
        "products": [
          {
            "name":  "Product 1" ,
            "tags": {
              "tag1": 2 ,
              "tag2": 1 ,
              "tag3": 1
            }
          }, {
            "name":  "Product 2" ,
            "tags": {
              "tag1": 1 ,
              "tag3": 1
            }
          }
        ],
        "user":  "100"
      } ,
      {
      "products": [
        {
          "name":  "Product 1" ,
          "tags": {
            "tag1": 1 ,
            "tag2": 1
          }
        }
      ] ,
      "user":  "200"
      }
    ]
    

    【讨论】:

    • 是的。这有点复杂。我确信有更好的方法来做到这一点。
    猜你喜欢
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 2018-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多