MongoDB 架构设计 - 查找按用户过滤的所有博客文章中的最后 X 条评论
我正在尝试使用 Morphia 和 Play 框架重现一个 Post
到许多 Comment
的经典博客模式。
我在 Mongo 中的架构是:
{ "_id" : ObjectId("4d941c960c68c4e20d6a9abf"),
"className" : "models.Post",
"title" : "An amazing blog post",
"comments" : [
{
"commentDate" : NumberLong("1301552278491"),
"commenter" : {
"$ref" : "SiteUser",
"$id" : ObjectId("4d941c960c68c4e20c6a9abf")
},
"comment" : "What a blog post!"
},
{
"commentDate" : NumberLong("1301552278492"),
"commenter" : {
"$ref" : "SiteUser",
"$id" : ObjectId("4d941c960c68c4e20c6a9abf")
},
"comment" : "This is another comment"
}
]}
我正在尝试向博客引入社交网络方面,因此我希望能够在 SiteUser
的主页上提供该 的最后 X 条评论SiteUser
的所有帖子中的好友。
我的模型如下:
@Entity
public class Post extends Model {
public String title;
@Embedded
public List<Comment> comments;
}
@Embedded
public class Comment extends Model {
public long commentDate;
public String comment;
@Reference
public SiteUser commenter;
}
根据我在其他地方读到的内容,我我认为我需要对数据库运行以下命令(其中 [a, b, c]
代表 SiteUser
):
db.posts.find( { "comments.commenter" : {$in: [a, b, c]}} )
我有一个 List
- 在 Morphia 中为
Comments.commenter
在Post
上设置索引, - 实际上构建了以上查询
I am trying to reproduce the classic blog schema of one Post
to many Comment
s using Morphia and the Play Framework.
My schema in Mongo is:
{ "_id" : ObjectId("4d941c960c68c4e20d6a9abf"),
"className" : "models.Post",
"title" : "An amazing blog post",
"comments" : [
{
"commentDate" : NumberLong("1301552278491"),
"commenter" : {
"$ref" : "SiteUser",
"$id" : ObjectId("4d941c960c68c4e20c6a9abf")
},
"comment" : "What a blog post!"
},
{
"commentDate" : NumberLong("1301552278492"),
"commenter" : {
"$ref" : "SiteUser",
"$id" : ObjectId("4d941c960c68c4e20c6a9abf")
},
"comment" : "This is another comment"
}
]}
I am trying to introduce a social networking aspect to the blog, so I would like to be able to provide on a SiteUser
's homepage the last X comments by that SiteUser
's friends, across all posts.
My models are as follows:
@Entity
public class Post extends Model {
public String title;
@Embedded
public List<Comment> comments;
}
@Embedded
public class Comment extends Model {
public long commentDate;
public String comment;
@Reference
public SiteUser commenter;
}
From what I have read elsewhere, I think I need to run the following against the database (where [a, b, c]
represents the SiteUser
s) :
db.posts.find( { "comments.commenter" : {$in: [a, b, c]}} )
I have a List<SiteUser>
to pass in to Morphia for the filtering, but I don't know how to
- set up an index on
Post
forComments.commenter
from within Morphia - actually build the above query
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
@Indexes(@Index("comments.commenter"))
放在Post
类上,或将@Indexed
放在 <Comment
类的 code>commenter 字段(Morphia 的Datastore.ensureIndexes()
将在类中递归并正确创建comments.commenter
code> Post 集合上的索引)我认为
ds.find(Post.class, "comments. “,用户)
中的评论者可以工作,ds
是一个数据存储
,而用户
是您的List
code> (不过我不使用@Reference
,所以我无法确认;你可能必须首先提取他们的Key
列表。Either put
@Indexes(@Index("comments.commenter"))
on thePost
class, or@Indexed
on thecommenter
field of theComment
class (Morphia'sDatastore.ensureIndexes()
will recurse in the classes and correctly create thecomments.commenter
index on thePost
collection)I think
ds.find(Post.class, "comments.commenter in", users)
would work,ds
being aDatastore
andusers
yourList<SiteUser>
(I don't use@Reference
though, so I can't confirm; you might have to first extract the list of theirKey
s).