MongoDB query execution different

Hi Team,

Can you please clarify the below 2 mongo commands. during executing different output

  1. command :

db.getCollection(‘june_2022_test’).find({"$and": [{“timeStamp”: {"$gt": ISODate(“2022-06-23T05:53:00.000”)}},{“timeStamp”: {"$lt": ISODate(“2022-07-09T04:47:00.000”)}},{ “publisher” : “N”}]}).count()
0

  1. Command :
    db.getCollection(‘june_2022_test’).find({“publisher”:“N”}).count()

the 2nd command execute is long time

why the different 1st and 2nd command what is reason

Many reasons.

  1. indexes
  2. number of documents examined, potentially less in #1
  3. both are combined

A COLLSCAN for all documents to determine if publisher:N is true vs an IXSCAN on the range of dates.

Only the explain plan can give the whole truth for sure, so please share.

1 Like