2016-04-28 138 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

相关问题