如何使用retrhinkdb获取json数据

时间:2018-09-28 17:27:44

标签: node.js date express group-by rethinkdb

(我不好)。 我使用一个组来包含日期。我想连续获取信息。我该怎么办

.group(r.row('time_create').dayOfWeek())

json导出

    [
        {
        group: 1,
        reduction: [
        {
        detail: "no",
        id: "37751c10-97ea-4a3a-b2c9-3e8b39383b79",
        order_id: "15",
        status: "Not_Delivery",
        time_create: "2018-09-23T15:25:13.141Z"
        }
       ]
      }
   ]

我想将数据json更改为

{
  "date":
  {
    "Sun": [
      {
            detail: "no",
            order_id: "15",
            status: "Not_Delivery",
            time_create: "2018-09-28 15:25:13"
    }
    ]
  }
}

我是否必须根据需要提供信息。

1 个答案:

答案 0 :(得分:0)

看起来像您尝试了一样,但是没有设法转换上一个问题中的数据。 ;)

这是一个命题,这不是做到这一点的唯一方法。

首先,您似乎要删除id字段。您可以使用without在ReQL中执行此操作:

.group(r.row('time_create').dayOfWeek()).without('id')

(您可以在without('id')之前申请group,它的工作原理应该相同,有关更多详细信息,请参见this。)

然后,将结果数组(称为queryResult)转换为对象(称为output):

// prepare the skeleton of the output
let output = {
  date: {}
};
// walk the result, filling the output in the process
queryResult.forEach((groupData) => {
  let key = groupData.group;
  if (!output[key]) {
    output[key] = [];
  }
  output.date[key].push(...groupData.reduction);
})

现在output中几乎有了所需的结构,唯一的事情是日期键仍然是数字,而不是简短的日期名称。我认为,这应该由前端处理,因为您可能希望为前端实现不同的语言。但是无论如何,想法始终是相同的:拥有一个转换表,该表将Rethink的日期编号与人类可读的日期名称对应起来:

const translationTable = {
  1: 'Mon',
  2: 'Tue',
  // ...
  7: 'Sun'
};

现在,如果您在前端执行此操作,则只需在需要显示时立即替换数据的键(或根据日期的显示方式从日期名称中检索键)。否则,如果您要使用后端实现(同样,这显然不是最佳解决方案),则可以在上面的代码中更改一行(假设您已经声明了translationTable):

let key = groupData.group;
// becomes
let key = translationTable[groupData.group];

请随时在评论中提问您是否不理解!