Query find() - Will anyone pls explain me this behaviour?

From: "'azzy _home' via mongodb-user" <mongodb-user@xxxxxxxxxxxxxxxx>
To: mongodb-user <mongodb-user@xxxxxxxxxxxxxxxx>
Date: Tue, 10 May 2016 09:43:09 -0700 (PDT)
Why ads?


Following are 4 similar queries to get count with find.


Query 1: Gives me wrong count (returns total documents count)

db.test.tourArrivals.find({
  "tour.stops.locationId": locationId
}).max({
  "scheduleInfo.stopSchedules.lastETAResult": "2016-05-04T15:29:37.9960000+02:00"
}).min({
  "scheduleInfo.stopSchedules.lastETAResult": "2016-05-04T15:29:10.8910000+02:00"
}).size();


Execution time: 0,1s


Query 2: explain() shows correct count returned???




db.test.tourArrivals.find({
  "tour.stops.locationId": locationId
}).max({
  "scheduleInfo.stopSchedules.lastETAResult": "2016-05-04T15:29:37.9960000+02:00"
}).min({
  "scheduleInfo.stopSchedules.lastETAResult": "2016-05-04T15:29:10.8910000+02:00"
}).explain("executionStats").executionStats.nReturned;


Execution time: 0,3s


Query 3: using find and $and with count




db.test.tourArrivals.find({
  $and: [
    {
      "scheduleInfo.stopSchedules.lastETAResult": {
        $gte: "2016-05-04T15:29:10.8910000+02:00"
      }
    },
    {
      "scheduleInfo.stopSchedules.lastETAResult": {
        $lte: "2016-05-04T15:29:37.9960000+02:00"
      }
    },
    {
      "tour.stops.locationId": locationId
    }
  ]
}).count();


Execution time: 0,8s


Query 4: same as 3, get count from explain()

db.test.tourArrivals.find({
  $and: [
    {
      "scheduleInfo.stopSchedules.lastETAResult": {
        $gte: "2016-05-04T15:29:10.8910000+02:00"
      }
    },
    {
      "scheduleInfo.stopSchedules.lastETAResult": {
        $lte: "2016-05-04T15:29:37.9960000+02:00"
      }
    },
    {
      "tour.stops.locationId": locationId
    }
  ]
}).explain("executionStats").executionStats.nReturned;

Execution time: 0,5s



My question:

1. Why are the explain("executionStats") faster than normal count()/size()?

2. Why does the 1. query return count of all document but the explain(..).executionStats.nReturned gives me correct count?



Output in NoSQL Manager:

// 10.05.2016 18:31:35
// Command #3:
// db.test.tourArrivals.find({"tour.stops.locationId":locationId}).max({"scheduleInfo.stopSchedules.lastETAResult":"2016-05-04T15:29:37.9960000+02:00"}).min({"scheduleInfo.stopSchedules.lastETAResult": "2016-05-04T15:29:10.8910000+02:00"}).size();
// Execution time: 0,1s
// Result:
13629

// 10.05.2016 18:31:35
// Command #4:
// db.test.tourArrivals.find({"tour.stops.locationId":locationId}).max({"scheduleInfo.stopSchedules.lastETAResult":"2016-05-04T15:29:37.9960000+02:00"}).min({"scheduleInfo.stopSchedules.lastETAResult": "2016-05-04T15:29:10.8910000+02:00"}).explain("executionStats").executionStats.nReturned;
// Execution time: 0,3s
// Result:
4638

// 10.05.2016 18:31:36
// Command #5:
// db.test.tourArrivals.find({$and: [
// {"scheduleInfo.stopSchedules.lastETAResult":{$gte: "2016-05-04T15:29:10.8910000+02:00"}},
// {"scheduleInfo.stopSchedules.lastETAResult":{$lte: "2016-05-04T15:29:37.9960000+02:00"}},
// {"tour.stops.locationId":locationId}
// ]}).count();
// Execution time: 0,8s
// Result:
4639

// 10.05.2016 18:31:36
// Command #6:
// db.test.tourArrivals.find({$and: [
// {"scheduleInfo.stopSchedules.lastETAResult":{$gte: "2016-05-04T15:29:10.8910000+02:00"}},
// {"scheduleInfo.stopSchedules.lastETAResult":{$lte: "2016-05-04T15:29:37.9960000+02:00"}},
// {"tour.stops.locationId":locationId}
// ]}).explain("executionStats").executionStats.nReturned;
// Execution time: 0,5s
// Result:
4639



Regards,

-- 
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
--- 
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@xxxxxxxxxxxxxxxx.
To post to this group, send email to mongodb-user@xxxxxxxxxxxxxxxx.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/955754d8-a369-4f25-bee8-52de74213138%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Why ads?