简体   繁体   English

如何在Mongoose中向架构属性添加选项?

[英]How do you add options to a schema property in Mongoose?

Say that my Schema is: 说我的模式是:

var fooSchema = new Schema({
  foo: String
});

and I want to add select: false to foo . 我想在foo添加select: false How would I do that? 我该怎么做? Without doing the following: 不执行以下操作:

var fooSchema = new Schema({
  foo: { type: String, select: false }
});

Can I do fooSchema.<somethingToAddSelectFalseToFoo> ? 我可以做fooSchema.<somethingToAddSelectFalseToFoo>吗?

I assume that this would work: Mongoose schema.set() function: http://mongoosejs.com/docs/api.html#schema_Schema-set . 我认为这会起作用:Mongoose schema.set()函数: http : //mongoosejs.com/docs/api.html#schema_Schema-set

Schema#set(key, [value])

Sets/gets a schema option.

Parameters:

key <String> option name
[value] <Object> if not passed, the current option value is returned

fooSchema.set('foo', { select: false }); fooSchema.set('foo',{select:false});

In a select query you could exclude it, ie fooSchema.find().select("-foo"). 在选择查询中,您可以排除它,即fooSchema.find()。select(“-foo”)。 Some examples are at https://stackoverflow.com/a/12097874/442472 . 一些示例位于https://stackoverflow.com/a/12097874/442472

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM