【问题标题】:mongoid query problemmongoid查询问题
【发布时间】:2011-07-10 22:31:07
【问题描述】:

我正在尝试在 mongoid (mongoid.org ORM) 中实现此功能:

 select * from parents 
 inner join children
 on parents.id = children.parent_id
 where children.created_at <= some_timestamp

父母嵌入孩子,孩子属于父母

到目前为止我没有任何问题:Parent.where(:child.exists => true),但我不知道我应该如何做相当于 'child.created_at'.lt => some_timestamp (对字符串使用方法是非法的。)

谢谢

【问题讨论】:

    标签: mongodb mongoid


    【解决方案1】:

    Mongoid 在内部使用 UTC 时间戳,要在查询中处理日期,您可以执行以下操作:

    :'child.created_at'.lt => Time.now.midnight.utc
    

    【讨论】:

    • 显然是这样的: Parent.where(:child.exists => true, 'child.created_at'.lt => Time.now.midnight.utc) 不起作用,因为它不可能在字符串上调用#lt。符号也不会削减它,因为 :child.created_at dot '.'在里面是非法的。有什么想法吗?
    • 好的,谢谢。或者:'child.creater_at'.lt。关于时间戳的提示很有用,我只需要更改它:)
    猜你喜欢
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 2014-05-19
    • 2016-10-02
    • 2011-09-01
    • 1970-01-01
    相关资源
    最近更新 更多