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

  • Home
  • SEARCH
  • 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 7811819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:18:22+00:00 2026-06-02T04:18:22+00:00

I want to try Mongo with Ruby. I connected, selected collection and I can

  • 0

I want to try Mongo with Ruby. I connected, selected collection and I can query data from MongoDB.

irb(main):049:0> coll.find_one({:x=>4})
=> #<BSON::OrderedHash:0x3fdb33fdd59c {"_id"=>BSON::ObjectId('4f8ae4d7c0111ba6383cbe1b'), "x"=>4.0, "j"=>1.0}>

irb(main):048:0> coll.find_one({:x=>4}).to_a
=> [["_id", BSON::ObjectId('4f8ae4d7c0111ba6383cbe1b')], ["x", 4.0], ["j", 1.0]]

But how to access propeties, when I retrieve BSON hash? I need something like this:

data.x
=> 4

to_hash method gives me the same BSON::OrderedHash… 🙁

  • 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-02T04:18:24+00:00Added an answer on June 2, 2026 at 4:18 am

    When you say coll.find_one({:x=>4}), you get a BSON::OrderedHash back that you access like a normal Hash:

    h = coll.find_one(:x => 4)
    puts h['x']
    # 4 comes out unless you didn't find anything.
    

    If you use a full find instead of find_one, you get a MongoDB::Cursor which is an Enumerable so you can iterate it like any other collection; the cursor will return BSON::OrderedHash instances as you iterate so you can do things like this:

    cursor = coll.find(:thing => /stuff/)
    cursor.each { |h| puts h['thing'] }
    things = cursor.map { |h| h['thing'] }
    

    If you wanted objects instead of Hashes then you’d have to wrap the MongoDB::Cursor and BSON::OrderedHash instances with object yourself (possibly via Struct).

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

Sidebar

Related Questions

If I want to try out something new in Ruby or Javascript, I love
from my small knowlegde from java se i want to try a tutorial on
I want to upsert a document with the mongo-ruby-driver using something like the following-
I am starting a new project, and I want to give mongodb a try.
I want to scale reads with MongoDB. To do this i can setup master-slave
I want try to get tennis players description from wikipedia without scratch the DOM
I want to try Mongodb w/ mongoengine. I'm new to Django and databases and
I want to try to filter all operators and operands including floating points from
I want to try out SICP with Python. Can any one point to materials
I want to try CMake to manage a new C++ project. However, some files

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.