Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your issue has more to do with parsing JSON or Ruby (Regexp) than with BSON. Your original question causes confusion by jumping directly to BSON. With the current Ruby driver, BSON is not directly exposed to the application writer, but mapped as naturally as possible from and to Ruby objects.</p> <p>JSON is strictly limited and safe for parsing. Adding parsing for Regexp moves beyond this.</p> <p>You can do what you want unsafely using Kernel#eval. This will parse your Regexp, but it will also parse exec, system, backticks, etc. For a public application with arbitrary user input, you will have to do something safer.</p> <p>Also note, the differences between the following lines, which highlights semantics with both Ruby and MongoDB:</p> <pre><code>{ interests: [ 'fishing', 'golf' ] } </code></pre> <p>The above exactly matches interests iff they are exactly [ 'fishing', 'golf' ]. no more, no less, no other order.</p> <pre><code>{ interests: { '$in' =&gt; [ 'fishing', 'golf' ] } } </code></pre> <p>The above matches interests if interests have either 'fishing' or 'golf', any order, any position, any extras. Note that the string key '$in' requires the original => syntax.</p> <p>Hope that this helps your understanding, and please feel free to followup with clarifying questions.</p> <p>The following is a working example.</p> <p>myapp.rb</p> <pre><code>require 'sinatra' require 'mongo' require 'json' include Mongo db = MongoClient.new().db('test') get '/' do if request[:query] query = eval CGI::unescape(request[:query]) docs = db.collection('test_collection').find(query).to_a.to_json "docs=#{docs}" end end </code></pre> <p>myapp_test.rb</p> <pre><code>require 'myapp' require 'test/unit' require 'rack/test' require 'open-uri' ENV['RACK_ENV'] = 'test' class MyAppTest &lt; Test::Unit::TestCase include Rack::Test::Methods def setup @db ||= Mongo::MongoClient.new['test'] @coll ||= @db['test_collection'] @coll.remove @coll.insert({name: 'john doe', interests: [ 'fishing', 'golf' ]}) end def app Sinatra::Application end def query_test(query) uri = "http://localhost:4567/?query=#{URI::encode(query)}" puts "uri=#{uri}" get uri puts last_response.body assert_match(/^docs=/, last_response.body) end def test_john_doe query_test("{ name: 'john doe'}") end def test_regexp query_test("{ name: /.*john.*/, interests: [ 'fishing', 'golf' ]}") end end </code></pre> <p>ruby -I. myapp_test.rb</p> <pre><code>Run options: # Running tests: uri=http://localhost:4567/?query=%7B%20name:%20/.*john.*/,%20interests:%20[%20'fishing',%20'golf'%20]%7D docs=[{"_id":{"$oid": "50e9e60029daeb0be1000001"},"name":"john doe","interests":["fishing","golf"]}] .uri=http://localhost:4567/?query=%7B%20name:%20'john%20doe'%7D docs=[{"_id":{"$oid": "50e9e60129daeb0be1000002"},"name":"john doe","interests":["fishing","golf"]}] . Finished tests in 0.065822s, 30.3850 tests/s, 60.7700 assertions/s. 2 tests, 4 assertions, 0 failures, 0 errors, 0 skips </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload