Reputation: 3579
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?
Upvotes: 0
Views: 1757
Reputation: 1604
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
Upvotes: 1