简体   繁体   中英

Pymongo: can I query a pymongo query object?

I have a query in python using pymongo that works very well. It returns entries for a single user. Later on the script, I want to narrow this results. I want to iterate over each day, and query again for this user how many entries are there on this day alone.

The question is: Do I need to make a new query, or can I query over the object I already have?

Thanks

Can't you simply filter the results that you have, instead of hitting the database again with a new query ?

todays_entries = [item for item in iterable if function(item)]

Explanation:

The result of your query will be a list of entries. I guess you are using that list in some way first, and later in the code you want to count all entries that was create today.

Python has a awesome way of filtering lists. Documentation for this can be found here:
Python Filter Function

The code I wrote above is just a more pythonic way of using that function. As I dont have your code I can only guess what it might look like for you. Maybe something like this:

todays_entries = [entry for entry in all_entries if entry.date == todays_date]

Ofcourse: entry.date has to be in a comparable format with todays_date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM