使用定义的标识符查询基本集合时,猫鼬投影错误

时间:2019-05-17 13:17:54

标签: mongoose dependency-injection schema projection discriminator

我已经在user.js中定义了模式User

let mongoose = require('mongoose');
let Schema = mongoose.Schema;
let UserSchema = new Schema({
    first_name: { type: String, required: false },
    last_name: { type: String, required: false },
    email: { type: String, required: true, unique: false  },
    password: { type: String, required: true  },
    role: { type: String, required: false },
    image: { type: Buffer, required: false },
    mfa_enabled: { type: Boolean, required: false },
    mfa_secret: { type:  String, required: false },
    mfa_skip:  { type: Boolean, required: false },
    user_groups: { type:  [String], required: false },
    login_attempts: { type: [Number], required: false }
});

module.exports = mongoose.model('User', UserSchema);

它有一个辨别器,在Patient.js中定义了患者:

let mongoose = require('mongoose');
let Schema = mongoose.Schema;
let User = require('./user');
let patientSchema = new Schema({
    age: { type: String },
    verified: { type: Boolean },
    overdue_assessment: { type: Date },
    dob: { type : Date },
    gender: { type : String },
    date_registered: { type: Date },
    last_assessment_score: { type: Number },
    last_assessment_date: { type: Date }
});

module.exports = User.discriminator('Patient', patientSchema);

我将Patient模型加载到端点(Patient.put.js)中,并尝试更新Patient。可以按预期工作:

let Patient = require('../../models/patient');


Patient.findById(req.tokenData.user_id, function(err, patient) {
 patient.last_assessment_date = new Date();
 patient.save();
});

现在,我尝试使用投影查询来查询我的基本模型User:

User.find(conditions, { password: false }, options, function(err, objects){
 // projection cannot have a mix of inclusion and exclusion.
});

删除projection参数可以消除错误并返回用户和患者,但是我无法将敏感的详细信息传输到客户端。

User.find(conditions, {}, options, function(err, objects){
 //success
});

此外,在我尝试查询User集合时,在Patient.put.js中删除require可以避免此项目错误。这对我来说很奇怪。

//let Patient = require('../../models/patient');


Patient.findById(req.tokenData.user_id, function(err, patient) {
 patient.last_assessment_date = new Date();
 patient.save();
});

我收到猫鼬错误:投影不能混合包含和排除。如何避免这种情况,以便在使用查询中的投影时从此查询返回用户和患者?为什么仅通过不相关端点中的require加载到Patient中会导致此问题?

0 个答案:

没有答案