Best practice for embedding an object in a document

36 views
Skip to first unread message

Shahaf Abileah

unread,
Dec 5, 2013, 8:29:19 PM12/5/13
to mong...@googlegroups.com
Hey everyone,

What's the recommended approach for embedding an object in a document?  I'd like to define the structure of the embedded document in the same manner as the parent Document, to get the same schema validation, and to have helper methods for my embedded type.

SchemaDocument looks promising, but I see a couple of problems with it:
  1. When you query the document out of Mongo, you don't get the original SchemaDocument type.  Looks like someone else ran into this issue and wrote code to illustrate it: https://friendpaste.com/2kZCdnwpbuOREzQHmTSurB.  Is it possible to have the query return original Embed type instead the dict?
  2. It looks like schema validation doesn't apply on the embedded document.  For example, I appended the following to the code from #1.  Is it possible to make validation apply on the embedded SchemaDocument?
# Insert a new document with an invalid type for 'embed'
doc2 = connection.Doc()
doc2.embed = 'hello'
doc2.save()

doc2 = connection.Doc.get_from_id(doc2._id)

print(type(doc2))
print(type(doc2.embed))

# The result of the print command is
# <class '__main__.Doc'>
# <type 'unicode'>
 
On the wiki (https://github.com/namlook/mongokit/wiki) I see a couple of other options:
  1. CustomType (https://github.com/namlook/mongokit/wiki/Structure).  Looks like this requires me to do my own serialization and doesn't allow me to reuse the same schema definition format (structure = {...}).
  2. DBRef (https://github.com/namlook/mongokit/wiki/DBRef).  Looks like with DBRef the data isn't really embedded, rather it's stored as a separate document, possibly in a separate DB, and is joined in on-demand.  This has a different performance characteristic.  For my use case, I'd prefer to embed.
Are there any other options I'm missing?  Which would you recommend for my scenario?  Where can I find documentation on SchemaDocument?

By the way, I'm new to mongokit and pretty new to mongo, so any help is doubly appreciated.

thanks!

--S

Reply all
Reply to author
Forward
0 new messages