假设我有一个看起来像这样的Mongoose架构:
userSchema = mongoose.Schema({
username: {
type: String,
isUnique: true,
required: true
},
teamSnap: {
username: String,
password: String
},
password: {
type: String,
required: true
},
linkedin_id: Number,
email: {
type: String,
required: true
},
numberOfLineupsCreated: Number,
hasPaidAlready: Boolean,
longitude: Number,
registered_at: {
type: Date,
default: Date.now
},
created_at: {
type: Date,
default: Date.now
},
updated_at: {
type: Date,
default: Date.now
}
});
如何告诉Mongoose不要将teamSnap字段保存/存储在数据库中?
答案 0 :(得分:1)
如果您不需要在数据库中保留字段,为什么在模型类中需要?
您可以稍后将其添加到从MongoDB查询的对象中。
db.users.findOne({id: id}, function(err, user) {
user.teamSnap = value
});
答案 1 :(得分:1)
您可以重新定义toJSON()
方法并删除您的字段:
UserSchema.methods.toJSON = function() {
var obj = this.toObject()
delete obj.teamSnap
return obj
}