How would I grab only articles with comments that were created 20 minutes ago?

前端 未结 2 431
借酒劲吻你
借酒劲吻你 2021-01-16 08:44

Using mongodb and mongoid. How would one grab all articles. Only articles with comments created 20 minutes ago?

class Article
  include Mongoid::Document

           


        
相关标签:
2条回答
  • 2021-01-16 09:29
    articles = Article.where(:_id.in => Comment.where(:created_at => 20.minutes.ago).map(&:article_id))
    
    0 讨论(0)
  • 2021-01-16 09:35

    you can also do this, which should be more efficient:

    articles = Article.where(:"comments.created_at".gt => 20.minutes.ago)
    
    0 讨论(0)
提交回复
热议问题