Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Create and update embedded documents with MongoEngine
# REF:
class Comment(EmbeddedDocument):
content = StringField()
name = StringField(max_length=120)
class Post(Document):
title = StringField(max_length=120, required=True)
author = StringField(required=True)
tags = ListField(StringField(max_length=30))
comments = ListField(EmbeddedDocumentField(Comment))
# Create a post:
post = Post(title="Quora rocks", author="Ross", tags=['tutorial', 'how-to'])
# Find a post
post = Post.objects.find(title="Quora rocks").first()
# Create a new comment
comment = Comment(content="Great post!", name="john")
# Add to comments list and save
# To editing existing comments you can use atomic updates[1] or simply change the comment and call (this will also do an atomic update).
# Update a comment in place
post = Post.objects.find(title="Quora rocks").first()
post.comments[0].name = "John"
# Or update with a set operation
post = Post.objects.find(title="Quora rocks", comment__name="john").update(set__comment__S__name="John)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment