0

When I run the following in the mongodb shell, it returns the correct number of records:

(searching for the matching "SourceName" within the last 8 days)

mongos> db.fdl.count({ SourceName : "SRC1" , "Created" : { $gte : new Date(new Date().setDate(new Date().getDate()-8))  }})
482

But when I try to run it through python / pymongo, it returns nothing:

import time
import pymongo

myclient = pymongo.MongoClient("mongodb://mylogin:mypass@mymongo.mydomain.net")
mydb = myclient['fdl']

print mydb.fdl.count({ SourceName : "SRC1" , "Created" : { $gte : new Date(new Date().setDate(new Date().getDate()-8))  }})

myclient.close()

then run it:

python query.py
0

In the python code, if I run the query:

    print mydb.fdl.count({ SourceName : "SRC1" })

Then I do get a value. So the problem comes from the $gte search.

The problem is not with the count() method, because if I use the find() method, the problem is the same: documents returned in the MongoDB shell, but nothing when calling with python.

Any help would be greatly appreciated ! Thanks a lot in advance :-)

  • ,Welcome to the site? What is the MongoDB version(x,y,z)? – Md Haidar Ali Khan Sep 16 '18 at 10:51
0

As per MongoDB pymongo documentation here

The count() method is deprecated and not supported in a transaction. Please use count_documents() instead.

count(with_limit_and_skip=False)

DEPRECATED - Get the size of the results set for this query.

The count() method is deprecated and not supported in a transaction. Returns the number of documents in the results set for this query. Does not take limit() and skip() into account by default - set with_limit_and_skip to True if that is the desired behavior. Raises OperationFailure on a database error.

When used with MongoDB >= 2.6, count() uses any hint() applied to the query. In the following example the hint is passed to the count command:

collection.find({‘field’: ‘value’}).hint(‘field_1’).count()

The count() method obeys the read_preference of the Collection instance on which find() was called.

Changed in version 3.7: Deprecated. Changed in version 2.8: The count() method now supports hint().

count_documents(filter, session=None, kwargs)

Count the number of documents in this collection.

The count_documents() method is supported in a transaction.

All optional parameters should be passed as keyword arguments to this method. Valid options include:

  • skip (int): The number of matching documents to skip before returning results.
  • limit (int): The maximum number of documents to count.
  • maxTimeMS (int): The maximum amount of time to allow this operation to run, in milliseconds.
  • collation (optional): An instance of Collation. This option is only supported on MongoDB 3.4 and above.
  • hint (string or list of tuples): The index to use. Specify either the index name as a string or the index specification as a list of tuples (e.g. [(‘a’, pymongo.ASCENDING), (‘b’, pymongo.ASCENDING)]). This option is only supported on MongoDB 3.6 and above.

The count_documents() method obeys the read_preference of this Collection.

Note When migrating from count() to count_documents() the following query operators must be replaced:

Operator     Replacement
$where        $expr
$near         $geoWithin with $center
$nearSphere $geoWithin with $centerSphere

$expr requires MongoDB 3.6+

For your further ref here and here

| improve this answer | |
  • Hello Haidar. Thank you so much for responding to my question and sending detailed information. The problem is not in count vs. count_documents() because even if I use the " find" method, I still get no records. So the problem is either on the use of $gte or "New Date() ...". Just to test, I replaced the query with print mydb.fdl.count_documents({ SourceName : "SRC1" , "Created" : { $gte : new Date(new Date().setDate(new Date().getDate()-8)) }}) And I am still getting 0 results. Whereas if I run the same query directly in the MongoDB shell, I get the correct number of records. – LoveLinux Sep 17 '18 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.