gpt4 book ai didi

ruby - 如何创建 Mongoid "within date range or nil"查询?

转载 作者:数据小太阳 更新时间:2023-10-29 07:53:10 26 4
gpt4 key购买 nike

我正在尝试为匹配特定日期范围或具有 nil 值的记录创建 Mongoid 查询。这是我的 ruby​​ 代码,它执行我想变成 Mongoid 查询的功能:

class MyModel
include Mongoid::Document
field :name
field :enabled, type: Boolean, default: false
field :start_date, type: DateTime
field :end_date, type: DateTime

def self.active
documents = where(enabled: true)
documents = documents.keep_if {|doc| doc.start_date.nil? || doc.start_date <= Date.today}
documents = documents.keep_if {|doc| doc.end_date.nil? || doc.end_date >= Date.tomorrow}
documents
end
end

如何通过将此方法转换为 Mongoid 查询来提高性能?

更新:

这是我用来验证正确行为的 RSpec 测试:

describe '.active' do
let!(:disabled){ Fabricate(:model, enabled: false, name: 'disabled') }
let!(:enabled_without_date){ Fabricate(:active_model, name: 'enabled_without_date') }
let!(:past){ Fabricate(:active_model, start_date: 1.week.ago, end_date: Date.yesterday, name: 'past') }
let!(:current){ Fabricate(:active_model, start_date: Date.today, end_date: Date.tomorrow, name: 'current') }
let!(:future){ Fabricate(:active_model, start_date: Date.tomorrow, end_date: 1.week.from_now, name: 'future') }

it 'returns only enabled and within the current time' do
MyModel.count.should == 5
models = MyModel.active.to_a
models.should_not be_empty
models.should_not include disabled
models.should_not include past
models.should_not include future
models.should include enabled_without_date
models.should include current
end
end

最佳答案

如果转换条件:

(start <= X OR start.nil?) AND (end >= Y OR end.nil?)

进入析取形式,你得到:

(start <= X AND end >= Y) OR (start <= X and end.nil?) OR (start.nil? and end >= Y) or (start.nil? and end.nil?)

然后您可以通过单个 $or 子句表达这一点:

$or: [
{:start_date.lte => start_date, :end_date.gte => end_date},
{:start_date => nil, :end_date.gte => end_date},
{:start_date.lte => start_date, :end_date => nil},
{:start_date => nil, :end_date => nil},
]

如果两个值都必须是 set 或 nil(也就是说,你不能有一个 set 和一个 nil),这就变得更简单了:

$or: [
{:start_date.lte => start_date, :end_date.gte => end_date},
{:start_date => nil},
]

为了满足您的规范,完整的查询将是:

Model.where({
:enabled => true,
:$or => [
{:start_date.lte => Date.today.to_time, :end_date.gte => Date.tomorrow.to_time},
{:start_date => nil},
]
})

关于ruby - 如何创建 Mongoid "within date range or nil"查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17663450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com