2016-04-28 103 views
0

我有一個包含400多項基本數據的單個集合的mongodb數據庫。將Rails應用程序連接到現有的Mongodb數據庫

我使用Rails和mongoid gem將兩者鏈接在一起,但是當我在rails控制檯中查詢我的模型時沒有找到任何條目。

QuizQuestion.first 

息率沒有結果

我的模型:

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

我已經配置了mongoid.yml配置文件指向數據庫的地址。

有誰知道如何正確地做到這一點或我要去哪裏錯了?

+0

什麼是收藏的名字? –

回答

1

爲什麼你看到沒有結果的原因:

1)數據庫的配置是不正確的,你都指向一個不同的數據庫在同一MongoDB實例

2)班的名稱不爲比賽名稱在mongo內收集。打開一個控制檯/終端類型:

mongo 

然後鍵入:

show dbs 

這是你在第一部分所需要的DBS的名稱

use x 

其中x是db名稱

show collections 

這會l是集合的名稱。

一旦你有你的收藏品的名稱,你可以添加到您的模型:

store_in collection: "name_of_collection_as_in_mongo" 

因此,如果您的集合的名稱是quiz_question如圖蒙戈客戶端,你可以做到這一點在你的模型:

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 

,你看不到任何記錄(如果你是在正確的數據庫名稱指向)的原因很可能是由於mongoid希望的類名等於一個pluralised集合名稱,以便QuizQuestions ==蒙戈內quiz_questions

+0

Thankyou。問題在於我的數據庫中的集合名稱被命名爲「生物學」,將其更改爲「quizQuestions」。 – ConorB

相關問題