猫鼬模式安全字段 [英] Mongoose Schema secured field
本文介绍了猫鼬模式安全字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
简短明了:有什么方法可以阻止设置架构字段,但允许获取值?
Short and clear: is there any way to prevent setting a schema field but allowing to get the value?
我一直在猫鼬文档周围,但是找不到我想要的东西.
I've been around the Mongoose Documentation but can't find what I'm looking for.
推荐答案
将字段定义为虚拟吸气剂,而不是传统领域.
Define the field as a virtual getter instead of a traditional field.
例如,假设您想通过Mongoose访问集合的pop
字段为只读:
For example, say you wanted to make the pop
field of your collection read-only when accessed via Mongoose:
var schema = new Schema({
city: String,
state: String
});
schema.virtual('pop').get(function() {
return this._doc.pop;
});
通过访问模型实例的私有_doc
成员,有可能在将来打破这种情况,但是当我刚刚对其进行测试时,这种方法可以正常工作.
By accessing the private _doc
member of your model instance it's possible this may break in the future, but this worked fine when I tested it just now.
这篇关于猫鼬模式安全字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文