为什么我的架构没有在mongoose数组中添加默认值? [英] Why doesn't my schema to add default values in mongoose arrays?

查看:114
本文介绍了为什么我的架构没有在mongoose数组中添加默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的架构:

var CustomUserSchema = new Schema({
    role: [],
    permissions: [],
});

权限字段存储一个字符串数组看起来像这样:

permissions field store an array of strings that looks like this:

["Delete", "Show","Create"]

role field存储一个如下所示的对象数组:

role field stores an array of objects that looks like this:

[
    {
        name:"admin",
        priority:10,
        permissions: ["Delete", "Show" , "update"]
    },
    {
        name:"user",
        priority:5,
        permissions: ["Delete", "Show"]
    }
]

现在,我的要求是能够将显示存储为架构中权限字段的默认值并存储' user'作为角色字段内名称的默认值,优先级优先级为 role 字段和'Show'对于权限角色字段内。

Now, my requirement is to be able to store "Show" as default value for permissions field in the schema and to store 'user' as default value for name inside of role field , priority 0 for priority inside of role field and 'Show' for permissions inside of role field.

尝试自己, 我来了以上:

Trying myself, I came up with this:

var CustomUserSchema = new Schema({
    role: [{
        name: {type: String, default: 'user'},
        priority:{ type: Number, default: 0 } ,
        permissions: [{type:String, default:'Show'}]
    }],
    permissions: [{type:String, default:'Show'}]
});

但它根本没有为字段分配默认值,而是给出一个大小为0的数组字段。

But it does not assign the default values to the fields at all and gives an array of size 0 to the fields .

以上架构出现了什么问题?
如何将这些存储为默认值?

What seems wrong with above schema? How do I store these as default values?

推荐答案

默认值实际上不适用于数组,除非是当然它是数组中的文档,你想在添加到数组时为该文档设置默认属性。

Default values really don't work with arrays, unless of course it is a document within the array and you want to set a default property for that document when added to the array.

因此,数组总是被初始化为空除非你故意在其中加入一些东西。为了实现您想要实现的目标,请添加预保存挂钩以检查空数组,然后放置其他位置其中的默认值:

Therefore an array is always initialized as "empty" unless of course you deliberately put something in it. In order to do what you want to achieve, then add a pre save hook that checks for an empty array and then otherwise places a default value in there:

var async = require('async'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/authtest');

var userSchema = new Schema({
  permissions:[{
    "type": String,
    "enum": ["Delete","Show","Create","Update"],
  }]
});

userSchema.pre("save",function(next) {
  if (this.permissions.length == 0)
    this.permissions.push("Show");

  next();
});

var User = mongoose.model( 'User', userSchema );

var user = new User();

user.save(function(err,user) {
  if (err) throw err;
  console.log(user);
});

这会创建空值:

{ __v: 0,
  _id: 55c2e3142ac7b30d062f9c38,
  permissions: [ 'Show' ] }

当然,如果您初始化数据或操作以在数组中创建条目:

If of course you initialize your data or manipulate to create an entry in the array:

var user = new User({"permissions":["Create"]});

然后你得到你添加的数组:

Then you get the array you added:

{ __v: 0,
  _id: 55c2e409ec7c812b06fb511d,
  permissions: [ 'Create' ] }

如果你想总是在权限中出现显示,那么对钩子进行类似的更改可以强制执行:

And if you wanted to "always" have "Show" present in permissions, then a similar change to the hook could enforce that for you:

userSchema.pre("save",function(next) {
  if (this.permissions.indexOf("Show") == -1)
    this.permissions.push("Show");

  next();
});

导致:

var user = new User({"permissions":["Create"]});

{ __v: 0,
  _id: 55c2e5052219b44e0648dfea,
  permissions: [ 'Create', 'Show' ] }

这些是您可以控制数组条目默认值的方法,而无需使用模型在代码中明确指定它们。

Those are the ways you can control defaults on your array entries without needing to explicitly assign them in your code using the model.

这篇关于为什么我的架构没有在mongoose数组中添加默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆