0

I am using a MongoDB on mLab to store a basic collection of boardgames which I wish to show in my Ruby app. I have completed a tutorial that uses Mongoid to implement this locally, but so far I can't get it working with the mLab instance of the DB.

I add this to my mongoid.yml file

development:
  clients:
    default:
      uri: 'mongodb://user:password@ds141232.mlab.com:41232/boardgame_banter'

The other options automatically generated in the config file, I have left blank (as default).

I want to understand these 2 lines from the Terminal:

MONGODB | ds141232-a.mlab.com:41232 | boardgame_banter.find | STARTED | {"find"=>"boardgames", "filter"=>{}}
MONGODB | ds141232-a.mlab.com:41232 | boardgame_banter.find | SUCCEEDED | 0.037816999999999996s


I get no errors, but also no documents returned and the generated index.html is blank...

Can anyone explain the first of the two lines MONGODB | ... to me, or at least confirm if my assumptions below are correct? Particularly the last part of the chain, is this telling me that the filtered results are empty?

MONGODB | <<hostname>> | <<database.find()>> | <<STATUS>> | {"find"=><<collection>>, "filter"=>{<<no results??>>}}


UPDATE after suggestion from @tfogo in the comments
In my controller:

  # GET /boardgames
  # GET /boardgames.json
  def index
    @boardgames = Boardgame.all
    @log = Boardgame.all.to_a
    puts "LOG: #{@log}"
  end

Which produces the following empty Log statement in the console:

    Started GET "/boardgames" for 127.0.0.1 at 2018-03-02 11:25:00 +0100
Processing by BoardgamesController#index as HTML
D, [2018-03-02T11:25:00.186878 #12983] DEBUG -- : MONGODB | ds141232-a.mlab.com:41232 | boardgame_banter.find | STARTED | {"find"=>"boardgames", "filter"=>{}}
D, [2018-03-02T11:25:00.223330 #12983] DEBUG -- : MONGODB | ds141232-a.mlab.com:41232 | boardgame_banter.find | SUCCEEDED | 0.035911000000000005s
LOG: [#<Boardgame _id: 5a984b439de90b3769420f2d, name: nil, rating: nil, minplayer: nil, maxplayer: nil, duration: nil, owner: nil>]
  Rendering boardgames/index.html.erb within layouts/application
D, [2018-03-02T11:25:00.235908 #12983] DEBUG -- : MONGODB | ds141232-a.mlab.com:41232 | boardgame_banter.find | STARTED | {"find"=>"boardgames", "filter"=>{}}
D, [2018-03-02T11:25:00.274734 #12983] DEBUG -- : MONGODB | ds141232-a.mlab.com:41232 | boardgame_banter.find | SUCCEEDED | 0.038311s
  Rendered boardgames/index.html.erb within layouts/application (42.3ms)
Completed 200 OK in 127ms (Views: 76.9ms)
coderwurst
  • 161
  • 3
  • 14
  • The filter property refers to the filter document not the results. The fact that filter is just {} doesn't mean you have no results, it just means your find query wasn't filtering the documents, i.e. it was searching for all the documents in the collection. Can you add some code to your controller to output the results of the query to see if you are receiving documents from the database? – tfogo Mar 01 '18 at 21:05
  • Thanks for the info! Updated the Question to include the Log statement - theres nothing there... – coderwurst Mar 02 '18 at 08:41
  • Queries are evaluated lazily in mongoid, so you still might be getting something back from the db, just you're printing before it arrives. If you do `@boardgames = Boardgame.all.to_a` and `puts "LOG: #{@boardgames}"`, what do you see? `.to_a` ensures the query is made before you log. – tfogo Mar 02 '18 at 10:11
  • Then I see LOG: [#] which is the collection id I can see in the mLab interface. The boardgames start after this - could this be a json formatting issue? – coderwurst Mar 02 '18 at 10:34
  • That's not a collection ID, it's a document ID. It does look like a formatting issue with your data in the database. The fact that all the fields printed are `nil` indicates the document returned by the DB doesn't match the Boardgame class you defined. The `puts` should output an array of all the documents in the collection, but you only have an array of one document. Do you expect to only have one document in the collection? – tfogo Mar 02 '18 at 10:37
  • Heres a [pastebin](https://pastebin.com/50HhuSuc) of the boardgames collection, which I imported with Robo 3T. The original data is stored in google sheets, which I then exported into json format. Im guessing the problem is with the conversions - each document (boardgame) should have 1 of these ids? – coderwurst Mar 02 '18 at 10:45
  • 1
    Yeah that's a single json document. You probably want each of those subdocuments to be separate documents. You want to import an array of documents. MongoDB will take care of giving an `_id` to each of the documents. – tfogo Mar 02 '18 at 10:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166094/discussion-between-coderwurst-and-tfogo). – coderwurst Mar 02 '18 at 11:26

0 Answers0