Method: Mongoid::Criteria#for_js

Defined in:
lib/mongoid/criteria.rb

#for_js(javascript, scope = {}) ⇒ Criteria

Find documents by the provided javascript and scope. Uses a $where but is different from Criteria#where in that it will pass a code object to the query instead of a pure string. Safe against Javascript injection attacks.

Examples:

Find by javascript.

Band.for_js("this.name = param", param: "Tool")

Parameters:

  • javascript (String)

    The javascript to execute in the $where.

  • scope (Hash) (defaults to: {})

    The scope for the code.

Returns:

Since:

  • 3.1.0



485
486
487
# File 'lib/mongoid/criteria.rb', line 485

def for_js(javascript, scope = {})
  js_query(BSON::CodeWithScope.new(javascript, scope))
end