【问题标题】:How to grab all articles with comments in mongodb?如何在mongodb中获取所有带有评论的文章?
【发布时间】:2012-05-11 02:53:37
【问题描述】:

如何使用 cmets 抓取所有文章?我有:

class Article
  include Mongoid::Document

  embeds_many :comments
end

class Comment
  include Mongoid::Document

  embedded_in :article
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mongodb mongoid


    【解决方案1】:

    目前。这似乎有效:

    Article.where(:comments => {"$exists" => true})
    

    我是否在此上下文中正确使用了 $exists 指令?

    【讨论】:

    • 是的,这是$exists的正确用法(假设没有cmets的文章根本没有这个字段,而不是有一个空数组)。
    【解决方案2】:

    存在 cmets 的文章(因此,不为零)

    Article.where(:comments.ne => nil)
    

    【讨论】:

      猜你喜欢
      • 2015-01-15
      • 2018-03-28
      • 1970-01-01
      • 2010-09-22
      • 2013-02-11
      • 1970-01-01
      • 2018-11-23
      • 2021-02-01
      • 2015-10-20
      相关资源
      最近更新 更多