[英]MongoDB - using Mongoose schema - how to tell Mongoose not to save particular field
Say I have a Mongoose schema that looks like this: 假设我有一个如下所示的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
}
});
how do I tell Mongoose NOT to save/store the teamSnap field in the database? 如何告诉Mongoose不要在数据库中保存/存储teamSnap字段?
If you don't need to persist a field in the database why do you need in your model class ? 如果您不需要在数据库中保留字段,为什么需要在模型类中?
You can add it later to the object that you query from MongoDB. 您可以稍后将其添加到您从MongoDB查询的对象中。
db.users.findOne({id: id}, function(err, user) {
user.teamSnap = value
});
You could redefine the toJSON()
method and remove your field there: 您可以重新定义
toJSON()
方法并在那里删除字段:
UserSchema.methods.toJSON = function() {
var obj = this.toObject()
delete obj.teamSnap
return obj
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.