Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$where operator in MongoDB query #6311

Closed
itaiperi opened this issue Feb 23, 2016 · 4 comments
Closed

$where operator in MongoDB query #6311

itaiperi opened this issue Feb 23, 2016 · 4 comments
Labels
confirmed We want to fix or implement it Project:Mongo Driver

Comments

@itaiperi
Copy link

_1 Upvote_ Hello,
I'm currently running Meteor:
version: 1.2.1
Platform: Linux Ubuntu 14.04.3
Github bug repository: https://github.com/noodlez04/meteor-mongodb-where-bug

The actual problem is this:
When using $where inside a mongodb query, functions do not work.
If i pass:
$where: function () { return false; } or $where: function () { return true; } they both work.
Trying to simply pass $where: 'false' and where: 'true' do work as expected.

The code used can be viewed in bug-reproducer.js in the root of my git repository.

Terminal output:

$ git init
Initialized empty Git repository in /media/Data/Projects/Learning/meteor/meteor-mongodb-where-bug/.git/
$ git add .
$ git commit -m "Bug reproduction on minimal Meteor project"
[master (root-commit) 292b9e6] Bug reproduction on minimal Meteor project
 8 files changed, 122 insertions(+)
 create mode 100644 .meteor/.finished-upgraders
 create mode 100644 .meteor/.gitignore
 create mode 100644 .meteor/.id
 create mode 100644 .meteor/packages
 create mode 100644 .meteor/platforms
 create mode 100644 .meteor/release
 create mode 100644 .meteor/versions
 create mode 100644 bug-reproducer.js
$ git remote add origin git@github.com:noodlez04/meteor-mongodb-where-bug.git
$ git push origin masterCounting objects: 11, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (11/11), 2.27 KiB | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To git@github.com:noodlez04/meteor-mongodb-where-bug.git
 * [new branch]      master -> master
$ meteor
[[[[[ /media/Data/Projects/Learning/meteor/meteor-mongodb-where-bug ]]]]]

=> Started proxy.                             
=> Started MongoDB.                           
=> Started your app.                          

=> App running at: http://localhost:3000/
I20160223-13:49:26.587(2)? Should print empty array: [ { _id: '12345678901234567', test_field: 'test' } ]
I20160223-13:49:26.588(2)? Should print document added: [ { _id: '12345678901234567', test_field: 'test' } ]
@zol zol assigned zol and unassigned zol Feb 23, 2016
@zol zol added Project:Mongo Driver confirmed We want to fix or implement it labels Feb 24, 2016
@zol zol removed their assignment Feb 24, 2016
@ozsay
Copy link
Contributor

ozsay commented Feb 27, 2016

+1

And it looks like it's only happening in the server environment

Another reproduction https://github.com/ozsay/meteor-where-issue

https://github.com/ozsay/meteor-where-issue/blob/master/whereIssue.js
I've printed the count of the queries to the console on some various finds.
server prints 2 1 1 2 and client 2 1 1 1.

@AnthonyAstige
Copy link

Passing the function as a string may work.

I've only briefly sanity checked, but this seems to be isomorphically working for me

var where = function() {
  return true;
};
// Convert to string on server for MongoDB Node.js driver as noted @ https://github.com/meteor/meteor/issues/6311
$where = (Meteor.isServer ? where + '' : where);

Reference

David Glasser's comment on Sep 16th 2013 in the Google Group meteor-talk @ Does mongodb $where operator not work with Meteor collections, even when used in server code?

So a confusing thing about MongoDB is that all of their docs are based on the "MongoDB shell", ie, what you get when you run "mongo". Which is JavaScript, yes, but it's actually entirely different from the JavaScript client for Node.js.

My impression is that the Node.js client (which is what Meteor uses) technically supports passing a function literal to $where, but only if you enable it by setting some serializeFunctions flag. And frankly... all that ends up doing is converting the function back to a string. I'd recommend even for non-Meteor Node uses to just use the string syntax, so you can see exactly what is being passed to the server. So I'm not sure that Meteor is likely to support the function syntax (which as I mentioned, doesn't work in other Node apps by default either).
...

@hwillson
Copy link
Contributor

As mentioned in #6311 (comment) (and in https://groups.google.com/forum/#!msg/meteor-talk/ErgCC4g_bB0/UAqlFpEA3g8J), Meteor does not currently support using function literals with $where (on the server). Using the $where function string syntax is supported however. So, this does not work:

Items.find({$where: function () {
  return this.owner === 'me';
}}).count());

but this does work:

Items.find({ $where: "this.owner === 'me'" }).count();

Getting function literals working with $where is a feature request.

To help provide a more clear separation between feature requests and bugs, and to help clean up the feature request backlog, Meteor feature requests are now being managed under the https://github.com/meteor/meteor-feature-requests repository.

This feature request will be closed here, but anyone interested in migrating this feature request to the new repository (to make sure it stays active), can click here to start the feature request migration process. This manual migration process is intended to help identify which of the older feature requests are still considered to be of value to the community. Thanks!

@FranzZemen
Copy link

Thank you. Using a function string syntax works with Mongo JavaScript 3.6 but not a function. Until I found this thread I was going nuts. And...as mentioned above both methods work in the mongo shell. Confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed We want to fix or implement it Project:Mongo Driver
Projects
None yet
Development

No branches or pull requests

6 participants