[SERVER-19387] Query validity not checked before execution Created: 14/Jul/15 Updated: 14/Jul/15 Resolved: 14/Jul/15 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Usability |
| Affects Version/s: | 3.0.3 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Pratik Patel | Assignee: | Sam Kleinman (Inactive) |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Attachments: |
|
| Operating System: | ALL |
| Steps To Reproduce: | Execute the following query on attached dataset (enron.zip): db.messages.aggregate([ , to:{$addToSet: '$headers.To'}}}, ,count:{$sum:1}}}, |
| Participants: |
| Description |
|
I was executing the below query on the enron dataset provided as part of MongoDB university training.
It came back after full 13 seconds complaining that there is a syntax errorL 2015-07-14T05:59:44.438-0400 E QUERY TypeError: Object #<Object> has no method 'explain' In my opinion validity of the query should be checked first before actually executing the query. Here it seems to have executed the query and then tried to execute the unavailable "explain" method which makes it look like an expensive mistake. Apologies if similar jira already exists or if this is filed under wrong project. |
| Comments |
| Comment by Pratik Patel [ 14/Jul/15 ] |
|
Thank you! |
| Comment by Sam Kleinman (Inactive) [ 14/Jul/15 ] |
|
Unfortunately the return type of JavaScript functions is not universally deducible before execution, and the language doesn't support that kind of error checking. You can connect to MongoDB instances with any driver, and while the shell is convenient for basic testing and exploration, there's no reason that you have to use a JavaScript environment and can easily use another driver and environment with more familiar semantics: consider the full list of drivers. For MongoDB-related discussion you can post on the mongodb-users group group Regards, |
| Comment by Pratik Patel [ 14/Jul/15 ] |
|
Thanks for the insight! I come from an SQL based database background where such errors would be flagged immediately, so hope you can understand why this behavior seemed strange to me! I of course don't want to re-open the issue given that I have recently started using MongoDB and don't have in depth understanding yet. But won't the return type of aggregate be a json document? Is it not possible for the shell (or any driver for that matter) to make a fair assumption about return types and enforce a check on syntax before approaching the server? Don't intend to frustrate you with my naive questions. Please let me know if I should take this discussion to a different Q&A type of forum. |
| Comment by Sam Kleinman (Inactive) [ 14/Jul/15 ] |
|
The error you're seeing is an error in the JavaScript shell. The shell sees a method chain, where each method executes and then calls the next method on the result of the first method, and so on. So the .aggregate() method executes, which takes 13 seconds in this case, and then pases its result to the .explain() method, which it discovers at runtime does not exist. Because the return type of the .aggregate() method isn't known until it returns, JavaScript and the Shell can't produce an error until after the function completes. I'm sorry for the frustration, and hope that this explanation is useful. Regards, |