我如何按照created_at的顺序列出所有评论?

时间:2021-11-13 12:13:21

I've got:

我有:

class Article
  include Mongoid::Document

  embeds_many :comments
end

class Comment
  include Mongoid::Document

  embedded_in :article
end

Since Comment is an embedded document in Article. How would I list down ALL Comments in order of created_at?

由于Comment是文章中的嵌入式文档。我如何按create_at的顺序列出所有评论?

Should I structure it so that Comment is not an embedded in Article or is there a way with the above schema?

我应该构建它,以便Comment不是嵌入在文章中,还是有上述架构的方法?

2 个解决方案

#1


1  

If you're trying to query comments out of context of their articles, then clearly comment should be a top level entity and not embedded one.

如果您试图在文章的上下文之外查询评论,那么显然评论应该是*实体而不是嵌入式实体。

In embedded case you can use map-reduce or aggregation framework. Regular query language won't help you here.

在嵌入式情况下,您可以使用map-reduce或聚合框架。常规查询语言对您没有帮助。

#2


0  

why not just default_scope :order => 'created_at DESC'? (or ASC).

为什么不只是default_scope:order =>'created_at DESC'? (或ASC)。

You're going to want them to be in a consistent order across the site anyway.

无论如何,您将希望它们在整个站点中保持一致的顺序。

#1


1  

If you're trying to query comments out of context of their articles, then clearly comment should be a top level entity and not embedded one.

如果您试图在文章的上下文之外查询评论,那么显然评论应该是*实体而不是嵌入式实体。

In embedded case you can use map-reduce or aggregation framework. Regular query language won't help you here.

在嵌入式情况下,您可以使用map-reduce或聚合框架。常规查询语言对您没有帮助。

#2


0  

why not just default_scope :order => 'created_at DESC'? (or ASC).

为什么不只是default_scope:order =>'created_at DESC'? (或ASC)。

You're going to want them to be in a consistent order across the site anyway.

无论如何,您将希望它们在整个站点中保持一致的顺序。