如何在 rethinkdb 的嵌套字段上使用 min/max

How do I use min/max on a nested field in rethinkdb

我正在尝试确定计算每个操作(一系列操作)所用时间的最佳方法。查看下面的示例数据,对于每个相应的操作,我如何为 "actions" 数组获取 min/max,其中包括 'take' 和 'throw' 操作:

{
  "name" : "test",
  "location" : "here",
  "operation" "hammer use",
  "actions" : [
    {
      "action" : "take",
      "object" : "hammer",
      "timestamp" : "12332234234"
    },
    {
      "action" : "drop",
      "object" : "hammer",
      "timestamp" : "12332234255"
    },
    {
      "action" : "take",
      "object" : "hammer",
      "timestamp" : "12332234266"
    },
    {
      "action" : "throw",
      "object" : "hammer",
      "timestamp" : "12332234277"
    }
},
{
  "name" : "test 2",
  "location" : "there",
  "operation" : "rock use",
  "actions" : [
    {
      "action" : "take",
      "object" : "rock",
      "timestamp" : "12332534277"
    },
    {
      "action" : "drop",
      "object" : "rock",
      "timestamp" : "12332534288"
    },
    {
      "action" : "take",
      "object" : "rock",
      "timestamp" : "12332534299"
    },
    {
      "action" : "throw",
      "object" : "rock",
      "timestamp" : "12332534400"
    },
{
  "name" : "test 3",
  "location" : "elsewhere",
  "operation" : "seal hose",
  "actions" : [
    {
      "action" : "create",
      "object" : "grommet",
      "timestamp" : "12332534277"
    },
    {
      "action" : "place",
      "object" : "grommet",
      "timestamp" : "12332534288"
    },
    {
      "action" : "tighten",
      "object" : "hose",
      "timestamp" : "12332534299"
    }
}

预期输出:

{
"operation" : "hammer use",
"elapsed_time" : 123
},
{
"operation" : "rock use",
"elapsed_time" : 123
}

我对 rethinkdb 还是个新手,正在努力掌握它。到目前为止,我提出了以下查询来从 table:

中选择我感兴趣的特定记录
r.db('test').table('operations').filter(function(row) {
  return row('actions').contains(function(x) { 
    return x('action').eq('take')}).and(
      row('actions').contains(function(x) { return x('action').eq('throw') })
  );
});

我仍在尝试弄清楚如何通过获取时间戳的 min/max 并将它们相互减去来汇总结果。

我希望那里有足够的细节来帮助您了解手头的目标。否则请告诉我。非常感谢任何帮助。

好吧,没人管这个,所以我不得不在没有任何帮助的情况下解决它。花了一点时间,但终于弄清楚了如何。这是在上面的嵌套字段中查找 min/max 和 elapsed_time:

的伪代码
r.db('test').table('operations').filter(function(row) { 
  return row('actions').contains(function(x) { return x('action').eq("take") }).and(
    row('actions').contains(function(x) { return x('action').eq("throw") })
  ); 
}).map(function(doc) {
  return { 
    operation: doc('operation'),
    min: doc('actions')('timestamp').min(), 
    max: doc('actions')('timestamp').max(),
    elapsed_time: doc('actions')('timestamp').max().sub(doc('actions')('timestamp').min())
  }
})