hmarr/mongoengine — pull request #590 by drudim (2013-10-20 16:48:23+00:00)
Title: GenericReferenceField serialization bug

Hi everyone!

I have found problem in GenericReferenceField to_mongo method. It returns dict, in which keys ordering isn't guaranteed. When you querying in mongodb with dictionary as argument, ordering does matter.

Quick example:

```
> db.test.insert({a: {b:1, c:2}})
> db.test.find({a: {c:2, b:1}})
> db.test.find({a: {b:1, c:2}})
{ "_id" : ObjectId("526404e0d00831db47ec2f8f"), "a" : { "b" : 1, "c" : 2 } }
````

And mongoengine quick example:

```
class Aggregator(Document):
title = StringField()
items = ListField(GenericReferenceField())

class Item(Document):
title = StringField()
```

Query:
```
Aggregator.objects.filter(items__all=[item])
```

This query will be translated to mongo query (where keys ordering is important) as:
```
{'items': {'$all': [{'_ref': DBRef('item', ObjectId('526406cbb23278816e57a712')), '_cls': 'Item'}]}}
```

I have provided test, which reproduce bug. Maybe, I have fixed just side effect of problem. Any way, I think you know this better :)

P.S. I understand, that if I replace "all" with "match" in this example, it will work correct.