本文介绍了使MongoDB userSchema在每个区域上具有特定特权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须制作一个页面,用于输入有关特定区域内人员的一些数据.每个用户将在特定区域享有特权.

I have to make a page for entering some data about people on specific regions. Each user will have prvileges on a specific region.

因此,我将两个集合放入一个数据库中.一种用于用户,另一种用于区域.

So I will make two collections into a DB. One for the users and one for the regions.

用户将只能访问一个区域(在某些情况下甚至可以访问更多区域)和特定的附件,例如读取或写入或读取和写入等.

The users will have access only to one region (or in some cases to more) and specific acceses, like read or write, or read&write, and so on.

如何为架构建模,以便可以添加更多区域?

How can I model my schema so I can add more regions?

这就是我的方法:

var mongoose = require("mongoose"), 
    passportLocalMongoose = require("passport-local-mongoose");

let userSchema = new mongoose.Schema({
    username:
    {type: String,
    unique: true
    },
    password: String,
    privileges:
    {
        regiune: [Number],
        read: [Number],
        write: [Number],
        edit: [Number]
    }
});

userSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("User", userSchema);

但是我不知道如何编辑该模型,以便它可以具有多个具有不同特权的区域.假设对于普通用户,他们可能只有一个区域具有读取,写入和编辑功能.但是,如果我希望其他用户可以访问具有读取,写入权限的区域x和具有写入权限的区域y,则无法使用此模式来做到这一点.

but I don't know how I should edit this model so it can have multiple regions with different privileges.For regular users they might have one region with read, write and edit, let's say. But if I want another user to have access to region x with read, write privileges and region y with write privileges I won't be able to do it using this schema.

那么我应该如何编辑呢? (也是,我是数据库的初学者,所以如果还有另一种更好的设置权限的方法,请告诉我)

So how should I edit this? (also, I'm a beginner in DBs so if there is another better way to set privileges please let me know)

谢谢!

推荐答案

实际上,由于特权字段属于每个用户,因此您可以轻松地拥有多个具有不同特权的区域

Actually, since the privileges field belongs to each user, you can easily have multiple regions with different privileges

将特权转换为数组,将每个特定的许可转换为数字,您就拥有了!

Turn privileges into an array and each particular permission into a number and you have it!

   privileges:
    [{
        regiune: Number,
        read: Number,
        write: Number,
        edit: Number
    }]

对象数组是在Mongo中建模依赖对象的惯用方式.人们通常建议不要将无边界数组作为子文档,但是我敢打赌,您的区域列表相对有限.

An array of objects is the idiomatic way to model dependent objects in Mongo. People generally recommend not having an unbounded array as subdocument, but I bet your list of regions is relatively finite.

您可能需要引入一些机制,以确保阵列中具有唯一的注册表. Mongo 4.10为此引入了一个插件: https://www.npmjs.com/package/您可以像这样使用mongoose-unique-array :

You will probably want to introduce some mechanism for ensuring that you have unique regiunes in the array. Mongo 4.10 introduced a plugin for this: https://www.npmjs.com/package/mongoose-unique-array that you can use like this:

privileges:
    [{
        regiune: { type: Number, unique: true },
        read: Number,
        write: Number,
        edit: Number
    }]

如果您想通过密钥(区域1,区域3等)作为对象访问权限,则可以将特权声明为混合类型,这意味着它可以是任意的json结构(但会丢失类型信息).或者,您可以使其成为一个单独的模型,并带有对用户对象的反向引用的集合.用户对象还可以具有对该新模型的引用列表(但它还是一个数组,而不是一个对象).

If you wanted to access your permissions as an object, by key: region1, region3, etc. Then you might declare privileges as a mixed type, meaning it can be any arbitrary json structure (but you lose type information). Or you might make it a separate model, collection with a back ref to the user object. The user object could also have a list of references to this new model (but it would again be an array and not an object).

这篇关于使MongoDB userSchema在每个区域上具有特定特权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 14:21