将MongoDB数据按Ignition字段值分组:大型数据集的解决方案

huangapple go评论60阅读模式
英文:

Grouping MongoDB Data by Ignition Field Values: A Solution for Large Datasets

问题

{
"我想根据"ignition"字段的值对数据进行分组。如果"ignition"的值为1,则所有值为1的记录应该在遇到下一个值为0之前被分组在一起,依此类推。": "我想根据"ignition"字段的值对数据进行分组。如果"ignition"的值为1,则所有值为1的记录应该在遇到下一个值为0之前被分组在一起,依此类推。",
"我在MongoDB中有86400条记录,我想查询数据以实现所需的输出。": "我在MongoDB中有86400条记录,我想查询数据以实现所需的输出。",
"数据如下:": "数据如下:",
"[": "[",
"{": "{",
"ignition": "点火",
"1": "1",
",": ",",
"time": "时间",
"112": "112",
"193": "193",
"}": "}",
",": ",",
"{": "{",
"ignition": "点火",
"1": "1",
",": ",",
"time": "时间",
"193": "193",
"}": "}",
",": ",",
"{": "{",
"ignition": "点火",
"0": "0",
",": ",",
"time": "时间",
"115": "115",
"}": "}",
",": ",",
"{": "{",
"ignition": "点火",
"1": "1",
",": ",",
"time": "时间",
"116": "116",
"}": "}",
",": ",",
"{": "{",
"ignition": "点火",
"1": "1",
",": ",",
"time": "时间",
"117": "117",
"}": "}",
",": ",",
"{": "{",
"ignition": "点火",
"1": "1",
",": ",",
"time": "时间",
"118": "118",
"}": "}",
",": ",",
"{": "{",
"ignition": "点火",
"0": "0",
",": ",",
"time": "时间",
"119": "119",
"}": "}",
",": ",",
"{": "{",
"ignition": "点火",
"1": "1",
",": ",",
"time": "时间",
"120": "120",
"}": "}",
",": ",",
"{": "{",
"ignition": "点火",
"1": "1",
",": ",",
"time": "时间",
"121": "121",
"}": "}",
",": ",",
"{": "{",
"ignition": "点火",
"1": "1",
",": ",",
"time": "时间",
"122": "122",
"}": "}",
",": ",",
"{": "{",
"ignition": "点火",
"0": "0",
",": ",",
"time": "时间",
"123": "123",
"}": "}",
"]": "]",
"\n": "\n",
"\n": "\n",
"我想要的输出如下:": "我想要的输出如下:",
"{": "{",
"time": "时间",
":": ":",
"[": "[",
"112": "112",
",": ",",
"193": "193",
"]": "]",
",": ",",
"time": "时间",
":": ":",
"[": "[",
"116": "116",
",": ",",
"117": "117",
",": ",",
"118": "118",
"]": "]",
",": ",",
"time": "时间",
":": ":",
"[": "[",
"120": "120",
",": ",",
"121": "121",
",": ",",
"122": "122",
"]": "]"
}

英文:

I want to group the data based on the values of the "ignition" field. If the "ignition" value is 1, all records with the value 1 should be grouped together until the next value of 0 is encountered, and so on.

I have 86400 records in MongoDB, and I want to query the data to achieve the desired output.

The data looks like this:

[
  {
    ignition: 1,
    time: 112        
  },
  {
    ignition: 1,
    time: 193        
  },     
  {
    ignition: 0,
    time: 115        
  },
  {
    ignition: 1,
    time: 116        
  },
  {
    ignition: 1,
    time: 117        
  },
  {
    ignition: 1,
    time: 118        
  },
  {
    ignition: 0,
    time: 119        
  },
  {
    ignition: 1,
    time: 120        
  },
  {
    ignition: 1,
    time: 121        
  },
  {
    ignition: 1,
    time: 122        
  },
  {
    ignition: 0,
    time: 123        
  },
]

I want the output like this:

{
  time: [112,193],
  time: [116,117,118],
  time: [120,121,122]
}

答案1

得分: 1

db.collection.aggregate([
  {
    $setWindowFields: {
      partitionBy: null,
      sortBy: {time: 1},
      output: {
        "groupNum": {
          $sum: {
            $cond: [  
              {$eq: ["$ignition", 1]}, 0, 1
            ]
          },
          window: {
            documents: ["unbounded","current"]
          }
        }
      }
    }
  },
  {
    $match: {"ignition": 1}
  },
  {
    $group: {
      _id: "$groupNum",
      time: {$push: "$time"}
    }
  },
  {
    $sort: {_id: 1}
  }
])
英文:
db.collection.aggregate([
  {
    $setWindowFields: {                         //6. the output of this stage is, each set of adjacent documents having same $ignition will have a unique groupNum
      partitionBy: null,
      sortBy: {time: 1},                        //4. from all documents sorted by $time
      output: {
        "groupNum": {                           //1. create a new field groupNum
          $sum: {                               //2. by cumulatively adding
            $cond: [  
              {$eq: ["$ignition",1]}, 0, 1      //3. modified $ignition field
            ]
          },
          window: {
            documents: ["unbounded","current"]  //5. starting from the beginning to current document
          }
        }
      }
    }
  },
  {
    $match: {"ignition": 1}                     //7. retain $ignition : 1
  },
  {
    $group: {
      _id: "$groupNum",                         //8. group by groupNum
      time: {$push: "$time"}                    //9. pushing the time to an array
    }
  },
  {
    $sort: {_id: 1}                             //10.sort as necessary
  }
])

Demo

huangapple
  • 本文由 发表于 2023年2月16日 11:08:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定