Arangodb Groupby查询多个字段 [英] Arangodb groupby query multiple fields

查看:413
本文介绍了Arangodb Groupby查询多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在arangodb集合中有一个产品数据库,其中产品具有多种尺寸.问题是,对于每种尺寸,都会重复使用相同的产品.但是每个产品都有一个共同的组号.像这样:

I have a database of products in an arangodb collection in which a product has multiple sizes. The issue is that for each size, the same product is repeated. But each product has a common group number. Like this:

{"name": "product1", "description": "someDescription", size: 5,price: 12 groupNumber: 12}
{"name": "product1", "description": "someDescription", size: 15, price: 26, groupNumber: 12}
{"name": "product1", "description": "someDescription", size: 25, price: 84, groupNumber: 12}
{"name": "product1", "description": "someDescription", size: 35, price: 106, groupNumber: 12}

{"name": "product2", "description": "someDescription", size: 5, price: 12, groupNumber: 11}
{"name": "product2", "description": "someDescription", size: 15, price: 22, groupNumber: 11}
{"name": "product2", "description": "someDescription", size: 25, price: 32, groupNumber: 11}
{"name": "product2", "description": "someDescription", size: 35, price: 43, groupNumber: 11}

我现在必须显示产品列表(在网页中),但是每个产品只能出现一次,并且每个产品的尺寸和价格都应排列在数组中,如下所示:

I have to now display the list of products(in a web page) but each product should appear only once with sizes and prices in an array for each product like this:

product1 someDescription sizes: 5,15,25,35, prices: 12,26,84,106
product2 someDescription sizes: 5,15,25,35, prices: 12,22,32,43
...

我该怎么做?

推荐答案

忽略groupNumber并按name分组,查询看起来像这样:

Ignoring the groupNumber and grouping by name, the query looks like this:

FOR p IN products
  COLLECT description = p.description, name = p.name INTO groups
  RETURN { 
    "name" : name, 
    "description": description,
    "prices" : groups[*].p.price,
    "sizes" : groups[*].p.size
  }

给出(更正后的)示例数据,查询将返回:

Given your (corrected) example data, the query returns:

[
  {
    "name": "product1",
    "description": "someDescription",
    "prices": [
      12,
      84,
      106,
      26
    ],
    "sizes": [
      5,
      25,
      35,
      15
    ]
  },
  {
    "name": "product2",
    "description": "someDescription",
    "prices": [
      43,
      32,
      22,
      12
    ],
    "sizes": [
      35,
      25,
      15,
      5
    ]
  }
]

未对分组的值进行排序,但是大小和价格的位置相对应,您可以缓解这一事实,将值压缩到大小价格图中:

The grouped values aren't sorted, but the positions of sizes and prices correspond, you can alleviate this fact to zip the values into a size-price map:

FOR p IN products
  COLLECT description = p.description, name = p.name INTO groups
  RETURN { 
    "name" : name, 
    "description": description,
    "size_price_map" : ZIP(groups[*].p.size, groups[*].p.price)
  }

屈服:

[
  {
    "name": "product1",
    "description": "someDescription",
    "size_price_map": {
      "5": 12,
      "15": 26,
      "25": 84,
      "35": 106
    }
  },
  {
    "name": "product2",
    "description": "someDescription",
    "size_price_map": {
      "5": 12,
      "15": 22,
      "25": 32,
      "35": 43
    }
  }
]

这篇关于Arangodb Groupby查询多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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