Fix #714, rebase version of #827

The original commit to fix #714 was months old, so I rebased the changes from the updated master branch.

All tests pass in my environment, except for two that I couldn't get to work in mongoengine/master either:

```
ERROR: test_queryset_aggregation_framework (tests.QuerySetTest)
----------------------------------------------------------------------
OperationFailure: command SON([('aggregate', u'person'), ('pipeline', [{'$match': {'age': {'$lte': 22}}}, {'$project': {'name': {'$toUpper': '$name'}}}]), ('cursor', {})]) failed: unrecognized field "cursor

ERROR: test_text_indexes (tests.IndexesTest)
----------------------------------------------------------------------
OperationFailure: text search not enabled
```