2012-01-31 10 views
0

_id = 4ef1fddbb33c45091d000000のセクションで、id = 1のテーマからすべてのメッセージを取得するにはどうすればよいですか?MongoDB(MongoEngine)でこの簡単なクエリをどのように行うことができますか?

私はいくつかのコードを使用し
{ 
    "_cls": "Section", 
    "_id": { 
     "$oid": "4ef1fddbb33c45091d000000" 
    }, 
    "_types": [ 
     "Section" 
    ], 
    "description": "Test description", 
    "themes": [ 
     { 
      "_types": [ 
       "Theme" 
      ], 
      "title": "Test", 
      "messages": [ 
       { 
        "content": "I'm content!", 
        "_types": [ 
         "Message" 
        ], 
        "id": 12, 
        "_cls": "Message" 
       }, 
       { 
        "content": "I'm second message!", 
        "_types": [ 
         "Message" 
        ], 
        "_cls": "Message", 
        "user": "inlanger", 
        "id": 13 
       } 
      ], 
      "content": "Test description", 
      "_cls": "Theme", 
      "id": 1 
     }, 
     { 
      "_types": [ 
       "Theme" 
      ], 
      "title": "Test2", 
      "messages": [ 
       { 
        "_types": [ 
         "Message" 
        ], 
        "create_date": { 
         "$date": "2012-01-31T11:29:17.120Z" 
        }, 
        "content": "Some message", 
        "_cls": "Message", 
        "id": 14, 
        "user": "inlanger" 
       } 
      ], 
      "content": "Test description 2", 
      "_cls": "Theme", 
      "id": 2 
     }, 
     { 
      "_types": [ 
       "Theme" 
      ], 
      "create_date": { 
       "$date": "2012-01-31T12:00:50.889Z" 
      }, 
      "title": "Ататата", 
      "messages": [], 
      "content": "Theme number 3", 
      "user": "inlanger", 
      "id": 15, 
      "_cls": "Theme" 
     } 
    ], 
    "title": "Test" 
} 

...それはの仕事が、それは醜いです::このように、いくつかのJSONを生成

class Message(EmbeddedDocument): 
    id = SequenceField(unique=True) 
    content = StringField() 
    create_date = DateTimeField() 
    user = StringField() 
    active = BooleanField() 

class Theme(EmbeddedDocument): 
    id = SequenceField(unique=True) 
    title = StringField() 
    content = StringField() 
    create_date = DateTimeField() 
    user = StringField() 
    messages = ListField(EmbeddedDocumentField(Message)) 
    active = BooleanField() 

class Section(Document): 
    title = StringField(unique=True) 
    description = StringField() 
    themes = ListField(EmbeddedDocumentField(Theme)) 

そして、このモデル:

私はいくつかのモデルを持っている

def get_theme_messages(section_id, theme_id, page = 1): 
    section = Section.objects(id = section_id, themes__id = theme_id).first() 
    for theme in section.themes: 
     if theme.id == int(theme_id): 
      return theme.messages 
      break 
     else: 
      pass 

答えて

3

MongoDBは、常に完全なドキュメント(つまり、Mongoengine用語でDocumentインスタンス)を返します。 DocumentのリストにEmbeddedDocumentのリストをフィルタリングする場合は、クライアント側のコードで行う必要があります(ここで示したように)。

+0

1あなたはあなたが得るトップレベルのフィールドを制限することができます(これは、あなたが上記の貼り付けたものに機能的に同等である)

def get_theme_messages(section_id, theme_id, page = 1): section = Section.objects(id = section_id, themes__id = theme_id).first() for theme in section.themes: if theme.id == int(theme_id): return theme.messages 

あなたは少しいくつかの不要な行を削除することによって、このコードをクリーンアップすることができますドキュメントから戻ってくる(つまり、メッセージを取得するだけですが)、この場合はあまり役に立ちません。編集:気にしない、トップレベルのフィールドではないことが分かります。 –

+0

そして何千ものレコードを持っていれば、それをすべて得るのは普通ですか? – inlanger

+0

何千もの 'EmbeddedDocument'sがあれば、そうです。最上位の 'Document'を返す場合は、必要以上にクエリをフィルタリングすることができます。 – dcrosta

関連する問題