【问题标题】:Doorkeeper::AccessToken + Mongoid: Query all expired tokensDoorkeeper::AccessToken + Mongoid:查询所有过期令牌
【发布时间】:2015-04-16 09:11:07
【问题描述】:

我有很多 access_tokens 存储在 Mongoid4 中。我想根据expires_in 字段(以秒为单位)和created_at 字段(时间类型)查询所有过期和未过期的令牌。

这两个字段如下所示:

field :created_at, type: Time
field :expires_in, type: Integer

我知道我可以像 Doorkeeper::AccessToken.where(created_at: (3.days.ago..Time.now)) 这样按日期查询。

但是如何在一个查询中比较 created_at 字段和 expires_in 字段?我想拥有所有访问令牌,其中created_at 字段加上来自expires_in 的秒数与Time.now 相比更小或更大。这甚至可能吗?还是我必须遍历所有条目,然后手动选择它们?

【问题讨论】:

    标签: mongodb mongoid access-token doorkeeper


    【解决方案1】:

    这里可以用 Mongoid 的 MapReduce 功能做一些事情,但一种简单的方法可能是首先选择 max expires_in 值,然后查询:

    Doorkeeper::AccessToken.where(created_at: (3.days.ago..Time.now-max_expires_in))
    

    它会为您提供您知道超出范围的令牌列表。

    那么你可以这样做:

    Doorkeeper::AccessToken.where(created_at: ((Time.now-max_expires_in)..Time.now))
    

    对于较小的列表,您必须手动过滤。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      相关资源
      最近更新 更多