我尝试使用mongoose实现基本模式继承。 基于http://mongoosejs.com/docs/api.html#model_Model.discriminator和https://github.com/Automattic/mongoose/pull/1647以及Inheriting Mongoose schemas。
当我尝试.find()在Person上时,它只会让我回到员工身上,这对我来说是一个问题,因为我期待员工和志愿者。
模式:
var mongoose = require('mongoose');
var util = require('util');
var Schema = mongoose.Schema;
function BaseSchema() {
Schema.apply(this, arguments);
this.add({
firstname: String,
lastname: String,
created_at: Date,
updated_at: { type: Date, default: Date.now }
});
}
util.inherits(BaseSchema, Schema);
var PersonSchema = new BaseSchema();
var EmployeeSchema = new BaseSchema();
EmployeeSchema.add({department: String});
var VolunteerSchema = new BaseSchema();
VolunteerSchema.add({shop: String});
var Person = mongoose.model('Person', PersonSchema);
var Volunteer = Person.discriminator('volunteer', VolunteerSchema);
var Employee = Person.discriminator('employee', EmployeeSchema);
module.exports = mongoose.model('Volunteer', VolunteerSchema);
module.exports = mongoose.model('Employee', EmployeeSchema);
我如何尝试让志愿者和员工:
var express = require('express'),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
Person = require('../models/person.js');
// ...
router.get('/', function(req, res, next) {
Person.find({}, function (err, persons) {
if (err) return next(err);
res.json(persons);
});
});
我如何添加'灯具'
var mongoose = require('mongoose'),
Person = require('../models/person.js'),
Employee = mongoose.model('Employee'),
Volunteer = mongoose.model('Volunteer');
var db = mongoose.connect('mongodb://127.0.0.1:27017/dbname')
.then(function() {
console.log('connection to db successful');
})
.catch(function(err) {
console.log(err);
});
Employee.create([
{
firstname: 'John',
lastname: 'Steinbeck',
department: 'Deprimos',
created_at: Date.Now
}, {
firstname: 'Terry',
lastname: 'Pratchett',
department: 'Fun',
created_at: Date.Now
}, {
firstname: 'Frank',
lastname: 'Herbert',
department: 'Mindblowing',
created_at: Date.Now
}, {
firstname: 'George R.R.',
lastname: 'Martin',
department: 'Sadistic',
created_at: Date.Now
}
], function(err, person) {
if (err) console.log(err);
else console.log(person);
});
Volunteer.create([
{
firstname: 'Zekiya',
lastname: 'Orson',
shop: 'Shop 1',
created_at: Date.Now
}, {
firstname: 'Gazorp',
lastname: 'Azorp',
shop: 'Shop 2',
created_at: Date.Now
}
], function(err, person) {
if (err) console.log(err);
else console.log(person);
});
感谢任何领导/线索/解决方案:)
答案 0 :(得分:0)
固定。
似乎我搞砸了/混淆了一下。 出口不好,进口不好。
也许这会对某人有所帮助:
var mongoose = require('mongoose');
var util = require('util');
var Schema = mongoose.Schema;
function PersonSchema() {
Schema.apply(this, arguments);
this.add({
firstname: String,
lastname: String,
created_at: Date,
updated_at: { type: Date, default: Date.now }
});
}
util.inherits(PersonSchema, Schema);
var personSchema = new PersonSchema();
var employeeSchema = new PersonSchema();
employeeSchema.add({department: String});
var volunteerSchema = new PersonSchema();
volunteerSchema.add({shop: String});
var Person = mongoose.model('Person', personSchema);
var Volunteer = Person.discriminator('volunteer', volunteerSchema);
var Employee = Person.discriminator('employee', employeeSchema);
module.exports = {
Person: Person,
Volunteer : Volunteer,
Employee : Employee
};
并检索:
var PersonModel = require('../models/person.model');
personsRouter.get('/', function(req, res, next) {
PersonModel.Person.find({}, function(err, employees) {
if (err) return next(err);
res.json(employees);
});
});