[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




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


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([ '' ], :database => 'test')
db = client.database
puts client.inspect
puts db.inspect

Would result in output like:

#<Mongo::Client:0x580 cluster=#<Cluster topology=Single[] servers=[#<Server address= 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


Thanks for the reply @Stennie.

As you mentioned, I am trying to retrieve documents within my database. However, upon calling the find function on my DB collection, I am still unable to see the relevant document output.
Below is my code snippet:

client = Mongo::Client.new('mongodb+srv://user:password@cluster.mongodb.net/test')
collection = client[:listingsAndReviews]
puts collection.find({name: "Ribeira Charming Duplex"})

The output which I was expecting to receive was this:

"Ribeira Charming Duplex"
"Fantastic duplex apartment with three bedrooms, located in the histori…"
"Privileged views of the Douro River and Ribeira square, our apartment …"
"Fantastic duplex apartment with three bedrooms, located in the histori…"

However i keep getting nil values.
The Database has been made public and hence you would be able to access it as well through mongoDB compass through the URI in my code snippet.

Thank you,
Pin Hoong

Hi @Pin_Hoong_Lai,

Thank you for providing the example output to clarify that you are just aiming to print the result document.

The find() method returns the results of the operation as an iterable CollectionView.

To get your desired output you need to iterate the result, for example:

# Print the first result
puts collection.find({name: "Ribeira Charming Duplex"}).first

# Print all results
collection.find({name: "Ribeira Charming Duplex"}).each do |document|
    puts document

For more examples please see the Ruby Driver Quick Start Tutorial.

Please remove the open access to your cluster. If you want to provide help for reproducing an issue, a sample document and the specific versions of software used should be a great starting point.


1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.