1

Morphia と Play フレームワークを使用して、1Post対多の古典的なブログ スキーマを再現しようとしています。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]はsを表しますSiteUser):

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

フィルタリングのためにモルフィアに渡す必要List<SiteUser>がありますが、方法がわかりません

  1. Morphia 内からPostforにインデックスを設定するComments.commenter
  2. 実際に上記のクエリを作成します
4

1 に答える 1

1
  1. クラスまたはクラスのフィールドのいずれかに配置@Indexes(@Index("comments.commenter"))します(Morphiaはクラスで再帰し、コレクションにインデックスを正しく作成します)Post@IndexedcommenterCommentDatastore.ensureIndexes()comments.commenterPost

  2. ds.find(Post.class, "comments.commenter in", users)はうまくいくと思いますdsDatastoreそしてusersあなたList<SiteUser>です(私は使っていません@Reference、それで私は確認できません;あなたは最初に彼らのリストを抽出しなければならないかもしれませんKey)。

于 2011-04-01T12:21:43.013 に答える