MongoEngine/mongoengine — pull request #826 by mmelliso (2014-12-04 20:53:36+00:00)
Title: EmbeddedDocumentListField (Closes #503)

Feedback and suggestions are always welcomed!

---

#### Commit Details

- Added new field type: EmbeddedDocumentListField.
- Provides additional query ability for lists of embedded documents.
- Closes MongoEngine/mongoengine#503.

Includes unit tests and updated documentation.

#### Breaking Changes

- None. New feature only.

#### Sample Usage

```python
from mongoengine import *

class Comments(EmbeddedDocument):
author = StringField()
message = StringField()

class BlogPost(Document):
comments = EmbeddedDocumentListField(Comments)

if __name__ == '__main__':

db = connect('EmbeddedDocumentListFieldTestCase')
BlogPost.drop_collection()

post1 = BlogPost(comments=[
Comments(author='user1', message='message1'),
Comments(author='user2', message='message1')
]).save()

post2 = BlogPost(comments=[
Comments(author='user2', message='message2'),
Comments(author='user2', message='message3'),
Comments(author='user3', message='message1')
]).save()

# -- [C]RUD -- #
post1.comments.create(author='user1', message='newly_created')
print BlogPost.objects(comments__message='newly_created').count() # 1

# -- C[R]UD -- #
filtered = post1.comments.filter(author='user1', message='message1').get()
print filtered.author # user1

filtered = post1.comments.exclude(author='user1').get()
print filtered.author # user2

# (With Chaining)
filtered = post2.comments.filter(author='user2').exclude(message='message2').get()
print filtered.message # message3

# -- CR[U]D -- #
# (With save method)
comment = post1.comments[0]
comment.author = 'user4'
comment.save()
print BlogPost.objects(comments__author='user4').count() # 1

# (With update method)
post2.comments.filter(author='user2').update(author='user5')
print BlogPost.objects(comments__author='user5').count() # 1

# -- CRU[D] -- #
post2.comments.filter(author='user5').delete()
print BlogPost.objects(comments__author='user5').count() # 0

post1.comments.exclude(author='user2').delete()
comment = post1.comments.get()
print comment.author # user2

db.drop_database('EmbeddedDocumentListFieldTestCase')
```