[Ruby] Unable to view collection/ db content

Hi, trying to retrieve contents of a client database through Ruby’s mongo documentation, however when outputting my collections via puts db and db[:collection name], the console returns an object of the form

#<Mongo::Database:0x000000010e726190>

or

#<Mongo::Collection:0x0000000109c63b30>

and not actual explicit values and its parameters. Does anyone know how to solve this?

Thanks

While this doesn’t answer your question, I noticed that the link you provided is to an older version (2.2) of the driver. Are you following along with that, are you using the most recent version’s (2.18) documentation?

1 Like

Hi @Pin_Hoong_Lai,

Calling puts on an object will print a stringified version of that object, as per your output.

You need to call appropriate methods like find() or aggregate() to retrieve documents:

Do you perhaps mean you want to see a representation of the object and instance values? You can use inspect for that:

require 'mongo'

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test')
db = client.database
puts client.inspect
puts db.inspect

Would result in output like:

#<Mongo::Client:0x580 cluster=#<Cluster topology=Single[127.0.0.1:27017] servers=[#<Server address=127.0.0.1:27017 STANDALONE pool=#<ConnectionPool size=0 (0-20) used=0 avail=0 pending=0>>]>>
#<Mongo::Database:0x600 name=test>

If those suggestions aren’t what you are looking for, please provide more details on your environment:

  • version of MongoDB Ruby driver
  • version of Ruby
  • version of MongoDB server
  • type of deployment you are connecting to
  • example of the output you are hoping to see

Regards,
Stennie