1

Recently, I wrote a tiny app to query data from MongoDB with pymongo. The code as following shown

colls = collection.find({'created': {'$gt': datetime.datetime(2014, 9, 14, 10),
                                     '$lt': datetime.datetime(2014, 9, 14, 17)}},
                        {'created':1, '_id': 0})
print 'Totally count is ', colls.count()

So far, everything is okay. However,

results = []
for item in colls:
    results.append(item)

I found that the length of results is less than colls.count(). It was so strange? Why they are different?

Jacky1205
  • 3,273
  • 3
  • 22
  • 44

1 Answers1

0

Unless you are running this in a sharded cluster and hitting this bug, your result suggests that there are more elements in the collection than are included in your find query. To verify, just re-run the query without parameters:

colls = collection.find({}, {'created':1, '_id': 0});

Then compare that length with the count results.

If they differ, then just switch your predicates to see what values are outside the given range:

colls = collection.find({'created': {'$lt': datetime.datetime(2014, 9, 14, 10),
                                     '$gt': datetime.datetime(2014, 9, 14, 17)}},
                        {'created':1, '_id': 0})

If that gets you nothing, and you have an index on created then you may have non-datetime values in the index and only one type will match when you query, so you could hint a different index to make sure they are returned.

Community
  • 1
  • 1
Adam Comerford
  • 21,336
  • 4
  • 65
  • 85