Mongoengine: dynamic Fields with EmbededDocuments as values
<p>I have been using MapField till now as:</p>

<pre><code>class Game(EmbeddedDocument):
    iscomplete = BooleanField()
    score = IntField()
    #other not dynamic fields


class Progress(Document):
    user = ReferenceField(User, dbref=True)
    games = MapField(EmbeddedDocumentField(Game))
    created_at = DateTimeField()
    updated_on = DateTimeField()
</code></pre>

<p>I need to convert games to a ReferenceField.</p>

<p>I want to create Document with dynamic fields/keys but embeddedDocument as the values, so that I can have a document like:</p>

<pre><code>{
    "game1": {
        "iscomplete": true,
        "score": 23,
        },
    "game2": {
        "iscomplete": false,
        "score": 10,
        }
}
</code></pre>

<p>Is t here anyway I can achieve it?</p>

<p>You can achive that using <a href="http://docs.mongoengine.org/guide/defining-documents.html#dynamic-document-schemas" rel="nofollow">dynamic document in mongengine</a>:</p>

<blockquote>
  <p>DynamicDocument documents work in the same way as Document but any
  data / attributes set to them will also be saved</p>
</blockquote>

<p>So, you remove the games field, and add later your dynamic field games as, game1, game2, etc fields, they will be saved.</p>

<pre><code>class Game(EmbeddedDocument):
    iscomplete = fields.BooleanField()
    score = fields.IntField()    

class Progress(DynamicDocument):
    user = ReferenceField(User, dbref=True)
    created_at = DateTimeField()
    updated_on = DateTimeField()

    p = Progress()
    p.game1 = Game(iscomplete=True, score=10)
    p.game2 = Game(iscomplete=False, score=5)
    p.save()
</code></pre>

python mongodb mongoengine embedded-documents