How to iterate MotorCursor pymongo.cursor.Cursor object?

3k Views Asked by At

I have a collection that I want to convert to list to use. I just can't get it done.

@tasks.loop(minutes=1)
    async def check_current_mutes(self):
        print("AAAAAA")
        mutes = utils.servicecode.IslaDB("Isla", "Muted")
        print(datetime.datetime.now())
        lists = mutes.test_collection.find({'unmute': {'$gte': str(datetime.datetime.now())}})
        print(lists)
        for muted in lists.to_list(length=100):
            print(muted)
            if muted["unmute"] == "Indefinite":
                return
            else:
                if not time.strptime(muted["unmute"]) <= str(datetime.datetime.now()):
                    member = await self.bot.get_user(muted)
                    print(member)

I just need to iterate it to compare values in it.

1

There are 1 best solutions below

0
Cristianjs19 On

I think the issue is that you must await to convert the object to list as following:

...
lists = mutes.test_collection.find({'unmute': {'$gte': str(datetime.datetime.now())}})
my_data_as_list = await lists.to_list(length=100)
for muted in my_data_as_list:
    print(muted)
...

That is the step you are missing:

my_data_as_list = await lists.to_list(length=100)