“基本模式”只返回一种类型而不是 2 (mongodb - mongoose)

Posted

技术标签:

【中文标题】“基本模式”只返回一种类型而不是 2 (mongodb - mongoose)【英文标题】:"Base schema" returns only one type instead of 2 (mongodb - mongoose) 【发布时间】:2017-05-30 11:00:05 【问题描述】:

我尝试使用 mongoose 实现基本模式继承。 基于http://mongoosejs.com/docs/api.html#model_Model.discriminator 和https://github.com/Automattic/mongoose/pull/1647 和Inheriting Mongoose schemas。

当我尝试对 Person 执行 .find() 时,它只会返回员工,这对我来说是个问题,因为我期待员工和志愿者。

架构:

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);
);

感谢任何线索/线索/解决方案:)

【问题讨论】:

【参考方案1】:

固定。

似乎我有点搞砸了。 糟糕的出口,糟糕的进口。

也许它会对某人有所帮助:

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);
  );
);

【讨论】:

以上是关于“基本模式”只返回一种类型而不是 2 (mongodb - mongoose)的主要内容,如果未能解决你的问题,请参考以下文章

b64toBlob()只返回大小和类型而不是完整的图像数据

mongo过期索引

Grails Mongo GORM 插件 - 浮点和字节的映射是字符串而不是数字

typeScrip泛型

如果列中的不同计数达到 10,则只返回 true/false,而不是检查 spark 中的行数

Java Script 数据类型