带有mongoose和node.js的多态用户模型

我正在使用mean.js样板进行应用程序。 这是一个用户作为雇主和候选人的招聘引擎。 现在,雇主和候选人应该作为单独的文件存储,每个用户只能有一个雇主或候选人,因为每个候选人或雇主都需要用用户对象login。

我在rails中使用postgresql做到了这一点:

## User belongs_to :profileable, :polymorphic => true ## Candidate and Employer has_one :user, :as => :profileable 

用mongoose,这是我迄今为止使用包“mongoose-schema-extend”所做的:

 var extend = require('mongoose-schema-extend'); var UserSchema = new Schema({ email: { type: String }, password: { type: String } }, { collection: 'users', discriminatorKey : '_type' }); var CandidateSchema = UserSchema.extend({ Experience: { type: Number } }); var EmployerSchema = user.user.extend({ companyName: { type: String } }); 

上面的代码工作,但在MongoDB只创build用户_type属性设置为候选人或雇主的文件。

我认为应该做的是将候选人,雇主和用户存储为单独的文件。 后来我不得不把雇主和公司和工作联系起来。 此外,我需要分别以不同的标准查询候选人和雇主。

什么是实现function的最佳方法?

scenairo可以处理与mognoose鉴别器。

根据mongoose文件:

鉴别器是一个模式inheritance机制。 它们使您能够在相同的基础MongoDB集合之上拥有多个具有重叠模式的模型。

Theres没有很多的教程,我可以find歧视,但是,我有一个链接,我觉得有用: http : //thecodebarbarian.com/2015/07/24/guide-to-mongoose-discriminators

谢谢

Interesting Posts