1
我有两个集合即Employees和Departments。在我使用的has_many和belongs_to的在MongoDB中使用关系
class Employee
include Mongoid::Document
field :name, type: String
field :age, type: Integer
field :empid, type: String
field :manager_id, type: Integer
field :department_id, type:Integer
attr_accessible :age, :empid, :name, :department_id, :manager_id
validates_presence_of :name
validates_uniqueness_of :empid
belongs_to :department
end
class Department
include Mongoid::Document
field :name, type: String
attr_accessible :name
has_many :employees
end
现在我想用员工指部门收集的_id,帮我
Mongo不是关系数据库,但它不是*反*关系。他们的关键是尝试和“嵌套”关系[例如部门班级将所有教师作为子对象]或者在代码中进行连接,而不是在数据库中进行连接。至少AFAIK。更多信息:http://www.mongodb.org/display/DOCS/Schema+Design如上所述,我会对此+1 :) – NPSF3000 2012-07-19 14:51:30
你是对的,但对于从mongo开始的人来说,最好让他们忘记关系模型并以面向文档的方式工作,那么当他们绝对需要关系时,他们可以以正确的方式进行。 – 2012-07-19 17:58:53