Mongodb Motor Find

Published 30 May, 2020


Easy way to use find in Mongodb Motor library

MongoDB keep amaze me about performance. Everytime I tries to do some heavy operation it returns the result immediately. Right now I’m going to explain how to use the find operation in MongoDB with Motor.

Motor is very similar to PyMongo but for latest Python. So if you’re using Python3.6 and chances are you will be using Aysnc, ASGI features. PyMongo is not support for keyword await and async operation. So MongoDB created this Motor package where you can perfor some async queries.

So what’s the problem with Find?

Find is very similar to PyMongo and it’s not required await keyword. But to get the value from it you have to use async.

I wanted to get all the task for the particular user using Motor in my Python Starlette ASGI framework application.

db.task.find({"username": username})

This is the query operation I need to perform. Even though if we are not provided the await argument it will return the async feature and we can to use asynchronous way to get the values from it.

One of the simplest way is to use async for loop. Here is the code that mongoDB find using motor library.

async def task_list(username):
    db = await get_database()
    tasks = [item async for item in db.task.find({"username": username})]

This article published under development on motor tags. Please support me by subscribing to my Newsletter. By subscribing you will not miss any future articles in this category.