class Thread(Queryable): __collection__ = 'threads' class Flags(Document): locked = Boolean() sticky = Boolean() hidden = Boolean() uploads = Boolean() class Comment(Identified, Reply): class Votes(Document): count = Integer() who = Array(ObjectId()) id = ObjectId('_id') message = String() vote = Embed(Votes) creator = ObjectId() updated = Date(default=utcnow, assign=True) uploads = Array(ObjectId()) # GridFS file references. class Continuation(Reply): continued = Reference() title = String() forum = Reference() replies = Array(Embed(Reply)) flag = Embed(Flags) stat = Embed(Statistics) subscribers = Array(ObjectId()) updated = Date(default=utcnow, assign=True)
class Links(Document): """Simplify construction of a JSON API "links object" due to use of Python reserved words. This aliases several names from the JSON representation to attributes more Python-friendly. """ obj = Embed(APILink, name='self') right = Embed(APILink, name='next') left = Embed(APILink, name='last') rel = Embed(APILink, name='related')
class Sample(Document): class Nested(Document): name = String() reference = ObjectId() id = ObjectId('_id') nested = Embed('.Nested', assign=True)
def __getitem__(self, name): """Allows for referencing specific array elements by index. For example, to check that the third tag is `baz`: Person.tag[3] == "baz" """ from marrow.mongo import Document, Field from marrow.mongo.field import Embed if self._combining: # If we are combining fields, we can not dereference further. raise KeyError() if self._field.__foreign__ != 'array': # Pass through if not an array type field. return self._field[name] if not isinstance(name, int) and not name.isdigit(): raise KeyError( "Must specify an index as either a number or string representation of a number: " + name) field = self._field._kind(self._document) if isinstance(field, Field): # Bare simple values. field = copy(field) field.__name__ = self._name + '.' + unicode(name) else: # Embedded documents. field = Embed(field, name=self._name + '.' + unicode(name)) return self.__class__(self._document, field)
class Sample(Document): class Embedded(Document): name = String() generic = Field() field = String('field_name') number = Number('field_name', default=27) array = Array(Number(default=42), name='field_name') embed = Embed(Embedded)
class Comment(Identified, Reply): class Votes(Document): count = Integer() who = Array(ObjectId()) id = ObjectId('_id') message = String() vote = Embed(Votes) creator = ObjectId() updated = Date(default=utcnow, assign=True) uploads = Array(ObjectId()) # GridFS file references.
class Forum(Queryable): __collection__ = 'forums' id = String('_id') # Redefine the primary key as a string slug. name = String() summary = String() # Permission tagsefer read = String() write = String() moderate = String() stat = Embed(Statistics) modified = Date()
class Sample(Document): class Embedded(Document): field = String() field = Embed(Embedded) alias = Alias('field.field')
class Embeds(Document): class Embedded(Document): field = String() foo = Embed(Embedded) bar = Embed(Embedded)
class Thread(Document): class Reply(Document): id = ObjectId() id = ObjectId() reply = Embed(Reply)
class Sample(Document): field = Embed(Point)