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

Inline javascript #1

Closed
michalmuskala opened this issue May 31, 2015 · 6 comments
Closed

Inline javascript #1

michalmuskala opened this issue May 31, 2015 · 6 comments

Comments

@michalmuskala
Copy link
Collaborator

MongoDB gives possibility to query using javascript. It is somewhat similar to fragment. Is there a possibility to use custom functions in queries or only the predefined ones? Is the function apporoach good here? It would look something like this:

from u in User,
     where: javascript("this.name == this.username")
@ericmj
Copy link
Collaborator

ericmj commented May 31, 2015

Mongo only accepts javascript in special locations (it's not a general query expression). Specifically it only seems to be allowed in mapReduce which ecto does not support (yet?) and $where [1].

We could could pass it directly as the value the driver expects (possibly wrapped for ecto), for example: %BSON.Javascript{code: "this.name == this.username"}, the javascript/1 function could return this,

[1] http://docs.mongodb.org/manual/core/server-side-javascript/

@michalmuskala
Copy link
Collaborator Author

It's possible to combine regular queries with javascipt [1].

It's basically possible to have a selector like this:
{ active: true, $where: "this.credits - this.debits < 0" }
to select all the documents with field active set to true, and that match the javscript expression.

[1] http://docs.mongodb.org/manual/reference/operator/query/where

@josevalim
Copy link
Contributor

When talking to Eric, we thought we could make javascript a value. So we would do:

js = Mongo.Ecto.javascript("some javascript code")
from q in Post, where: p.title == "foo" and ^js

@ericmj ?

@michalmuskala
Copy link
Collaborator Author

I was thinking about making something like that, and providing all the functions like javascript, regex, etc. under Mongo.Ecto.Query module, you could import.
You could then do something like:

from q in Post, where: ^javascript("this.content.length < 100")

@josevalim
Copy link
Contributor

@michalmuskala well, if it is a type, I wouldn't necessarily put in Mongo.Ecto.Query because they can theoretically be used elsewhere. Can any of regex or javascript be saved as a value for a field in the database?

@michalmuskala
Copy link
Collaborator Author

I tried implementing this to support the syntax I mentioned earlier, but I hit a problem with Ecto.

From my understanding it seems that Ecto expects elements passed in where clause to be always convertible to boolean values. I see two solution for this: either modify Ecto to support passing other values to where, or make a new Ecto type to represent javascript that would respond to typecasting to a boolean, but return the BSON.JavaScript struct, we would use later in building query.

cesarizu pushed a commit to cesarizu/mongodb_ecto that referenced this issue Nov 20, 2016
fix update_all and insert_all tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants