hmarr/mongoengine — pull request #415 by amcgregor (2012-01-25 22:12:31+00:00)
Title: Ability to have scalar returns instead of partially-popuated Document instances.

In my current use case I have one embedded document with a field containing an ID, and two other collections with similar IDs and different names. I needed to `itertools.chain()` the result of three queries, but only get the desired ID _value_ as a result. (I also needed to retain the ability to `len()` the queries and not consume all records, so comprehensions were out).

The result is the attached pull request, usable like this:

```python
from mongoengine import *

connect('foo')

class Bar(EmbeddedDocument):
age = IntField()

class Foo(Document):
name = StringField()
detail = EmbeddedDocumentField(Bar)

Foo.drop_collection()

a = Foo(name="Foo", detail=Bar(age=27)) ; a.save()
b = Foo(name="Bar", detail=Bar(age=30)) ; b.save()
c = Foo(name="Baz", detail=Bar(age=4)) ; c.save()

print list(Foo.objects.scalar('name'))
# ['Foo', 'Bar', 'Baz']
print list(Foo.objects.scalar('detail__age'))
# [27, 30, 4]
print list(Foo.objects.scalar('name', 'detail__age'))
# [('Foo', 27), …]
```

It's a rough implementation (e.g. probably doesn't handle missing values well or at all) but it works quite well for my needs.