【问题标题】:How do you add options to a schema property in Mongoose?如何在 Mongoose 中为模式属性添加选项?
【发布时间】:2015-11-13 13:12:39
【问题描述】:

假设我的架构是:

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

我想将select: false 添加到foo。我该怎么做?不执行以下操作:

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

我可以fooSchema.<somethingToAddSelectFalseToFoo>吗?

【问题讨论】:

    标签: javascript mongoose


    【解决方案1】:

    我认为这会起作用: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.find().select("-foo")。一些示例位于https://stackoverflow.com/a/12097874/442472

    【讨论】:

    • 是的,我在文档中看到了,但我不明白。你能确认/解释一下吗?
    猜你喜欢
    • 2011-09-22
    • 2020-07-04
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    相关资源
    最近更新 更多