Skip to content

Conversation

@wojcikstefan
Copy link
Member

@wojcikstefan wojcikstefan commented May 8, 2017

Fixes #563 and closes #1002.

This PR introduces tweaks to QuerySet.as_pymongo - a method that hasn't been touched in at least a couple years. Part of me wants to get rid of it entirely and suggest that people use Doc._get_collection().find(...) if they need raw PyMongo documents, but perhaps that's too drastic of a change (and can be revisited when we make _get_collection public, see #1182).

This PR significantly simplifies the code and gets rid of an arguably useless coerce_types kwarg (the idea of converting values to MongoEngine-specific types in a method that's supposed to return raw PyMongo documents confuses me greatly). For now I also followed the previous logic of excluding _cls as an implementation detail and also excluding _id if it was only fetched for internal MongoEngine purposes.

Even though I suspect this to be barely used, it introduces a breaking change and should hence be released along with a major version bump.


This is off-topic, but I was curious and performed a test of how much faster as_pymongo is compared to the regular queryset iteration:

In [12]: class User(Document):
    ...:     name = StringField()
    ...:     email = EmailField()
    ...:

In [13]: for i in range(100000):
    ...:     User.objects.create(name='Steve ' + str(i), email='steve%d@gmail.com' % i)
    ...:

In [14]: def test():
    ...:     for doc in User.objects:
    ...:         pass
    ...:

In [15]: def test2():
    ...:     for doc in User.objects.as_pymongo():
    ...:         pass
    ...:

In [16]: %time test()
CPU times: user 8.75 s, sys: 179 ms, total: 8.93 s
Wall time: 8.89 s

In [17]: %time test2()
CPU times: user 463 ms, sys: 37.8 ms, total: 500 ms
Wall time: 517 ms

Quite a difference...

@wojcikstefan wojcikstefan merged commit 3b88712 into master May 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Combining only and as_pymongo fails to return values for a GenericReferenceField

2 participants