Best practices for logging queries generated by MongoEngine

1,981 views
Skip to first unread message

richardhenry

unread,
Jun 24, 2010, 11:52:03 AM6/24/10
to MongoEngine Users
I'd like to see the queries that MongoEngine generates while my
application runs. Is it possible to do this on either the MongoEngine
or PyMongo level? If it is possible, what is the best practice for
doing so?

Richard

Harry

unread,
Jul 7, 2010, 8:40:10 AM7/7/10
to MongoEngine Users
Hi Richard,

Sorry for the late reply - I've been away for a few weeks.

You should be able to call 'explain' on a QuerySet to see the query
that has been generated:

>>> User.objects(name__startswith='a').explain()
{u'allPlans': [{u'cursor': u'BtreeCursor _types_1', u'indexBounds':
[[{u'_types': u'User'}, {u'_types': u'User'}]]}, {u'cursor':
u'BasicCursor', u'indexBounds': []}], u'millis': 0, u'n': 1,
u'cursor': u'BtreeCursor _types_1', u'oldPlan': {u'cursor':
u'BtreeCursor _types_1', u'indexBounds': [[{u'_types': u'User'},
{u'_types': u'User'}]]}, u'indexBounds': [[{u'_types': u'User'},
{u'_types': u'User'}]], u'nscannedObjects': 2, u'nscanned': 2}

Hope this helps,

Harry

richardhenry

unread,
Jul 8, 2010, 2:49:43 AM7/8/10
to MongoEngine Users
Hey Harry,

Thanks for the reply. It would be great if you could pass connect() a
logging object and it would log queries as they are made, but
explain() is definitely useful.

We've mostly been using MongoDB's profiling, but I can't help but feel
that just seeing raw queries as they are sent to the DB wouldn't be
more helpful for debugging (and even just scanning through the list of
ops looking for anomalies -- "real" queries are easier to read).

Anyway, thank you for your help and hard work on MongoEngine.

Richard

Jason S. Wang

unread,
Jul 25, 2013, 4:28:16 AM7/25/13
to mongoeng...@googlegroups.com
It's late reply and I am not sure if you still need it or not.
I was brought here because I had exactly the same need when I am developing application using MongoEngine.
I had no luck with google search results.
However, I did a little code tracing and I found that the generated query can be accessed by following code:

cursor = User.objects(name__startswith='a')
print cursor._query      # This will print the query generated by MongoEngine
print cursor.explain()   # This will get you the profiling info from MongoDB regarding the current query

Hope that it helps with anyone who tried to search for this piece of information.
I am using MongoEngine 0.8.x

Jason
Reply all
Reply to author
Forward
0 new messages