Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9089179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:05:16+00:00 2026-06-16T22:05:16+00:00

Is there a way to pass a BSON object directly into the .find() in

  • 0

Is there a way to pass a BSON object directly into the .find() in the mongo-ruby-driver?

At the moment I have a basic sinatra app that takes URL encoded JSON and parses it into the .find() but I would ideally like to give it straight BSON:

require 'sinatra'
require 'mongo'
require 'json'

include Mongo
db = MongoClient.new().db('test')

get '/' do
  if request[:query]
    query = JSON.parse(CGI::unescape(request[:query]))
    db.collection('test_collection').find(query).to_a.to_json
  end
end

So essentially have something along the lines of BSON.parse(url-encoded-query) and be able to pass that into a .find() returning the result.

Example URL: http://localhost:4567/?query=%7B%20%22name%22%20%3A%20%22john%20doe%22%20%7D
Current query: { "name" : "john doe" }
BSON query: { name: /.*john.*/, interests: [ 'fishing', 'golf' ]} that I’d like to work

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:05:17+00:00Added an answer on June 16, 2026 at 10:05 pm

    The following test script demonstrates how to use the $elemMatch operator as a projection. Please note that the Collection#find method takes arbitrary documents for both the “selector” formal parameter and for the “opts” :fields option.

    MongoDB documents are mapped to/from Ruby Hash objects, and these documents can fully incorporate MongoDB operators.

    elemmatch_projection.rb

    #!/usr/bin/env ruby
    
    # Ruby translation of example from http://docs.mongodb.org/manual/reference/projection/elemMatch/
    require 'mongo'
    coll = Mongo::MongoClient.new['test']['students']
    coll.remove
    coll.insert({
      zipcode: 63109,
      dependents: [
        { name: "john", school: 102, age: 10 },
        { name: "jess", school: 102, age: 11 },
        { name: "jeff", school: 108, age: 15 }
      ]
    })
    p coll.find( { zipcode: 63109 }, :fields => { dependents: { '$elemMatch' => { school: 102 } } } ).to_a
    

    ruby elemmatch_projection.rb

    [{"_id"=>BSON::ObjectId('50eab29929daeb05ae000001'), "dependents"=>[{"name"=>"john", "school"=>102, "age"=>10}]}]
    

    This is another answer because the question has been significantly clarified by the OP.

    Hope that this helps you understand how to use MongoDB documents and operators in Ruby.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to pass a view model(s) into a function? We have
Is there a way to pass results generated within a PHP page (called into
Is there a way to pass a variable into a regex in jQuery/Javascript? I
Is there a way to pass a class into a property as a Class
Is there a way to pass a newly created object(not saved yet) to another
is there any way to pass a datetime directly in this format? mydate =
is there a way to pass external parameter into Grid renderer function? For example,
Is there a way to pass named arguments to a Ruby script? I understand
Is there any way to pass this into an Immediately-Invoked Function Expression, without resolving
Is there a way to pass a variable into a method param: <h:commandButton value=Add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.