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 7165687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:17:25+00:00 2026-05-28T14:17:25+00:00

I am trying ‘mongo-ruby-driver’ for some project. It’s working fine except when I call

  • 0

I am trying ‘mongo-ruby-driver’ for some project. It’s working fine except when I call to_json on mongo object. It gives well formed json but it’s not converting BSON::Code into readable value i resulting JSON.

Instead of showing code text, it show something like

#<BSON::Code:0x00000100af6fa8>

Did anyone tried it. Any help id highly appreciated.

UPDATE

here is some code snippet:

@records is variable that contains Array of MongoDB documents in hash.

@records.to_json

When I call to_json on it it gives everything as expected. except for the key that contain BSON::Code (means javascript code). for example consider following doc:

{ 
  "_id" : "contains", 
  "value" : function( obj, target ) { return obj.indexOf(target) != -1; }; 
}

Querying same doc from ruby gives output like:

{
  "_id"=>"contains", 
  "value"=><BSON::Code:2160165280 @data="function( obj, target ) { return obj.indexOf(target) != -1; };" @scope="{}">
}

and calling to_json on this gives following:

{"_id":"contains","value":"#<BSON::Code:0x00000100b54658>"}

this is what the problem is. Instead of getting actual code for ‘value’ key I am getting ruby object as string.

Calling code method on BSON::Code we can get it converted into code. But for that I need to loop mongo docs, check values for each key, calling code on it if it is an object of BSON::Code and then assigning it back to key. And at last we can call to_json on it. But I dont want this much overhead. I need to_json itself should take care of it.

  • 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-05-28T14:17:26+00:00Added an answer on May 28, 2026 at 2:17 pm

    The serializer that is iterating over the attributes of the objects in the array is probably calling the to_s method which would cause the output you are seeing.

    You can either monkey patch the BSON::Code class to include a to_s method which calls inspect (the method that produces the output you want) or modify the serializer to detect when it encounters a BSON::Code instance and call inspect on it rather than to_s.

    The code to mokey patch the BSON::Code class would look like this:

    module BSON
      class Code
        def to_s
          inspect
        end
      end
    end
    

    This would have the same behavior as inspect. If you just wanted the code you could monkey patch this in:

    module BSON
      class Code
        def to_s
          @code
        end
      end
    end
    

    I might make this the default behavior for to_s in the driver but for now just include that in your code and it should work like a champ.

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

Sidebar

Related Questions

Trying to parse an HTML document and extract some elements (any links to text
Trying to use GnuPG with Delphi (Win32). I need to sign some file with
Trying to get comfortable with jQuery and I have encountered some sample code that
Trying to read in some information from the google maps api into my application
Trying to find some information on this but am unable to get any results
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to keep all the presentation stuff in the xhtml on this project and
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to learn HTML5 and downloaded a test project https://github.com/amiklosi/Comicr . There is a

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.