Pymongo queries the first 1w data of a specific condition, and the count result is the number of records in the entire collection. What was wrong with me?

  mongodb, question

The code is as follows:
recode1 = table_out.find({}).sort([(“_id”,1)]).limit(10000)
Print (‘total:’, recode1.count ())
Directly understand that the count () number print should be 10,000, but the current output is 126w of all records in the entire collection. please answer, thank you very much.

Mongo’scursor.count()Method is ignored by defaultcursor.skip()Andcursor.limit()The effect of, and directly returnfind()The matching result of the method. if it is required to consider limit, it is required to specifyapplySkipLimitParameters aretrue.
In pymongo, this parameter corresponds to the in the methodwith_limit_and_skipParameters. Don’t bother to look through the document, the following is the function definition directly extracted from source code:

def count(self, with_limit_and_skip=False):

To be specific to your question, just write this:

recode1 = table_out.find({}).sort([("_id",1)]).limit(10000)
 Print ('total:', recode 1.count (with _ limit _ and _ skip = true))

Reference:Mongo official document: cursor.count ():

applySkipLimit: boolean
Optional. Specifies whether to consider the effects of the cursor.skip() and cursor.limit() methods in the count. By default, the count() method ignores the effects of the cursor.skip() and cursor.limit(). Set applySkipLimit to true to consider the effect of these methods.