【问题标题】:Best way to group by date with Mongoid使用 Mongoid 按日期分组的最佳方式
【发布时间】:2017-07-06 15:07:32
【问题描述】:

我正在尝试使用不同格式(日、月、年)按日期分组查询,我知道在典型的 SQL 数据库上这是一个简单的查询。

你可以在这个链接上看到代码:https://gist.github.com/jrdi/b3f824fa4e7531c43bfd

知道我可以跑:

> Patient.group_by('created_at', 'day')
=> [{"_id":"11/10/2013","value":{"count":3.0}}] 

在我看来,所有这些用于制作简单分组的代码真的很奇怪。我错过了什么重要的东西吗?

PD:我知道 self.map 方法和一些插值不是最好的方法,但我知道我关心 Mongo 的东西。

【问题讨论】:

  • 看起来你有与这个答案相似的代码stackoverflow.com/a/12034204/1354978
  • @earlonrails 是的,当然,但这意味着这是一个很好的解决方案?我只是在乎,因为我看到我的(或你的链接)代码真的很奇怪。

标签: ruby mongodb nosql


【解决方案1】:

这是一个使用聚合框架按日期分组的答案。我希望你喜欢它。

app/model/patient.rb

class Patient
  include Mongoid::Document
  field :name, type: String

  def self.group_by(field, format = 'day')
    key_op = [['year', '$year'], ['month', '$month'], ['day', '$dayOfMonth']]
    key_op = key_op.take(1 + key_op.find_index { |key, op| format == key })
    project_date_fields = Hash[*key_op.collect { |key, op| [key, {op => "$#{field}"}] }.flatten]
    group_id_fields = Hash[*key_op.collect { |key, op| [key, "$#{key}"] }.flatten]
    pipeline = [
        {"$project" => {"name" => 1, field => 1}.merge(project_date_fields)},
        {"$group" => {"_id" => group_id_fields, "count" => {"$sum" => 1}}},
        {"$sort" => {"count" => -1}}
    ]
    collection.aggregate(pipeline)
  end
end

test/unit/patient_test.rb

require 'test_helper'
require 'pp'

class PatientTest < ActiveSupport::TestCase
  def setup
    Patient.delete_all
  end

  test "group by date" do
    [
        {"name" => "John", "created_at" => Date.new(2012, 10, 10).mongoize},
        {"name" => "Jane", "created_at" => Date.new(2012, 10, 31).mongoize},
        {"name" => "Mary", "created_at" => Date.new(2012, 10, 31).mongoize},
        {"name" => "Mark", "created_at" => Date.new(2012, 12, 12).mongoize},
        {"name" => "Alex", "created_at" => Date.new(2013, 11, 10).mongoize},
        {"name" => "Andy", "created_at" => Date.new(2013, 10, 31).mongoize},
        {"name" => "Toni", "created_at" => Date.new(2013, 10, 31).mongoize},
        {"name" => "Cori", "created_at" => Date.new(2013, 11, 10).mongoize}
    ].each do |patient|
      Patient.create(patient)
    end
    puts "\nMongoid::VERSION:#{Mongoid::VERSION}\nMoped::VERSION:#{Moped::VERSION}"
    pp Patient.group_by('created_at', 'month')
  end
end

$ rake 测试

Run options:

# Running tests:

[1/1] PatientTest#test_group_by_date
Mongoid::VERSION:3.1.5
Moped::VERSION:1.5.1
[{"_id"=>{"year"=>2012, "month"=>10}, "count"=>3},
 {"_id"=>{"year"=>2013, "month"=>10}, "count"=>2},
 {"_id"=>{"year"=>2013, "month"=>11}, "count"=>2},
 {"_id"=>{"year"=>2012, "month"=>12}, "count"=>1}]
Finished tests in 0.042561s, 23.4957 tests/s, 0.0000 assertions/s.
1 tests, 0 assertions, 0 failures, 0 errors, 0 skips

【讨论】:

  • 在此查询中包含条件的最佳方式是什么?它工作正常,但我需要限制为特定的:post_id。该怎么做?
  • 只需在管道的开头添加一个 $match 阶段,请参阅docs.mongodb.org/manual/reference/operator/aggregation/match
  • 太棒了。我在测试中看到的第一个 SO 答案。
猜你喜欢
  • 2015-04-17
  • 1970-01-01
  • 2013-12-27
  • 2020-09-16
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 2015-03-26
相关资源
最近更新 更多