我正在尝试使用 Morphia 和 Play Framework 重现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"
}
]}
我正在尝试在博客中引入社交网络方面,因此我希望能够在 aSiteUser
的主页上提供该SiteUser
朋友在所有帖子中的最后 X 条评论。
我的模型如下:
@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
s):
db.posts.find( { "comments.commenter" : {$in: [a, b, c]}} )
我有一个List<SiteUser>
要传递给 Morphia 进行过滤,但我不知道如何
- 在 Morphia中
Post
为 for设置索引Comments.commenter
- 实际构建上述查询