[SERVER-11803] $where clause invoked twice Created: 20/Nov/13  Updated: 10/Dec/14  Resolved: 21/Nov/13

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.5.4
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: A. Jesse Jiryu Davis Assignee: hari.khalsa@10gen.com
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

db.collection.drop();
db.collection.insert({_id: 1, foo: 'bar'});
db.collection.find({
  _id: 1,
  foo: 'bar',
  $where: function() {
      print('$where run with: ' + tojson(obj));
      return true;
  }
});

This logs:

2013-11-20T18:02:41.551-0500 [conn9] $where run with: { "_id" : 1, "foo" : "bar" }
2013-11-20T18:02:41.553-0500 [conn9] $where run with: { "_id" : 1, "foo" : "bar" }

If you remove the _id and foo filters from the query, then the JS function only runs once.

Participants:

 Description   

If a $where clause is combined with other filters in a query, the Javascript function in the $where clause is invoked twice.



 Comments   
Comment by hari.khalsa@10gen.com [ 21/Nov/13 ]

If we're running two queries in order to compare them and select the best, I think this works as intended.

Comment by A. Jesse Jiryu Davis [ 21/Nov/13 ]

Looks like newRunQuery tries two plans, thus invoking the JS function twice. This is surprising for driver tests that need a query to be slow, and expected a query like this:

findOne({_id: 1, $where: function() { sleep(1000); return true; }})

... always to take about one second, not two.

When a $where clause is used for its intended purpose, rather than for a side effect, this is less of an issue.

Comment by A. Jesse Jiryu Davis [ 20/Nov/13 ]

Bad behavior starts at this commit:

commit 45a9b3929a1f8ce20188e0979be9fae6ac67fd2a
Author: Hari Khalsa <hkhalsa@10gen.com>
Date:   Sat Oct 12 09:45:39 2013 -0400
 
    SERVER-10026 enable new query system by default

Generated at Thu Feb 08 03:26:47 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.