1

我正在尝试使用 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]代表SiteUsers):

db.posts.find( { "comments.commenter" : {$in: [a, b, c]}} )

我有一个List<SiteUser>要传递给 Morphia 进行过滤,但我不知道如何

  1. 在 Morphia中Post为 for设置索引Comments.commenter
  2. 实际构建上述查询
4

1 回答 1

1
  1. 要么放在@Indexes(@Index("comments.commenter"))类上Post,要么放在类@Indexedcommenter字段上Comment(Morphia将在类中递归并在集合上Datastore.ensureIndexes()正确创建comments.commenter索引)Post

  2. 我认为ds.find(Post.class, "comments.commenter in", users)作为dsaDatastoreusers你的List<SiteUser>(虽然我不使用@Reference,所以我无法确认;你可能必须先提取他们Key的 s 列表)。

于 2011-04-01T12:21:43.013 回答