0

I have a mongodb database with a single collection containing 400+ entries of basic data.

I'm using Rails and the mongoid gem to link the two together however when I query my model in the rails console there are no entries found.

 QuizQuestion.first

Yields no results

My model:

class QuizQuestion
  include Mongoid::Document
  field :question, type: String
  field :correctAnswer, type: String
  field :wrongAnswers, type: Array, default: []
  field :category, type: String
end

I have configured the mongoid.yml configuration file to point to the address of the database.

Does anyone know how to correctly do this or where I'm going wrong?

1
  • What is the collection's name? Commented Apr 28, 2016 at 21:20

1 Answer 1

1

The reasons why you see no result:

1) database config is incorrect and you are pointing to a different database on the same mongodb instance

2) class name does not match the name for the collection within mongo. Open up a console/terminal and type:

mongo

then type this:

show dbs

This is the name of the dbs you need in the first part

use x

Where x is the db name

show collections

This will list the names of the collections.

Once you have the name of your collections, you can add this to your model:

store_in collection: "name_of_collection_as_in_mongo"

Therefore if the name of your collection was quiz_question as shown in the mongo client you can do this on your model:

class QuizQuestion
  include Mongoid::Document
  store_in collection: "quiz_question"
  field :question, type: String
  field :correctAnswer, type: String
  field :wrongAnswers, type: Array, default: []
  field :category, type: String
end

The reason you are not seeing any records (if you are pointing at the correct db name) is most likely due to mongoid expecting the class name to equal a pluralised collection name so QuizQuestions == quiz_questions within mongo

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou. The issue was that collection name in my database was named 'biology', changing it to 'quizQuestions' worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.