仅包含与另一条路径的值相关的 Mongoose 模式的路径

Posted

技术标签:

【中文标题】仅包含与另一条路径的值相关的 Mongoose 模式的路径【英文标题】:Include only the path of Mongoose schema which is relevant to value of another path 【发布时间】:2020-04-10 10:04:37 【问题描述】:

假设我有一个这样的架构:

Schema(
    username: String,
    privilege: 
        type: String,
        enum: ["admin", "pro", "user", "guest"],
        required: false,
        default: "user"
    ,
    tagType:  // A little badge that appears next to users' names on the site.
        ///???
    
);

对于tagType,如果privilegeadmin,我希望能够定义这样的内容:


    color: String,
    image: String,
    text: 
        type: String,
        required: true,
        enum: ["I'm the highest rank", "I'm an admin", "Admin privileges are great"]
    ,
    dateAdded: Date

同时,如果privilegepro


    color: String,
    text: 
        type: String,
        required: false,
        enum: ["This website is great", "I'm am a Pro user", "I get a nice tag!"],
        default: "This website is great"
    ,
    dateAdded: Date

同时,对于所有 userguest 条目,不应允许任何选项。

这种动态交叉检查可能吗?我曾尝试通读docs,但尽管其中的内容看起来要复杂得多,但我找不到任何方法来实现这一点。

【问题讨论】:

【参考方案1】:

你可以使用Discriminators来实现这个

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const userSchema = new Schema(
  
    username: String,
    privilege: 
      type: String,
      enum: ["admin", "pro", "user", "guest"],
      required: false,
      default: "user"
    
  ,
   discriminatorKey: "privilege" 
);

const User = mongoose.model("User", userSchema);

User.discriminator(
  "admin",
  new Schema(
    tagType: 
      color: String,
      image: String,
      text: 
        type: String,
        required: true,
        enum: ["I'm the highest rank","I'm an admin","Admin privileges are great"]
      ,
      dateAdded:  type: Date, default: Date.now 
    
  )
);

User.discriminator(
  "pro",
  new Schema(
    tagType: 
      color: String,
      text: 
        type: String,
        required: false,
        enum: ["This website is great","I'm am a Pro user","I get a nice tag!"],
        default: "This website is great"
      ,
      dateAdded:  type: Date, default: Date.now 
    
  )
);

module.exports = User;

使用此模型和架构,您可以创建一个带有 tagType 的用户,它将被忽略。

创建用户的示例代码:

const User = require("../models/user");

router.post("/user", async (req, res) => 
  let result = await User.create(req.body);
  res.send(result);
);

示例请求正文:


    "username": "user 1",
    "tagType": 
        "color": "red",
        "text": "I'm an admin"
    

回复:


    "privilege": "user",
    "_id": "5dfa07043a20814f80d60d6b",
    "username": "user 1",
    "__v": 0

创建管理员的示例请求:(注意我们添加了"privilege": "admin"


    "username": "admin 1",
    "privilege": "admin",
    "tagType": 
        "color": "red",
        "text": "I'm an admin"
    

响应:(注意tagType已保存)


    "_id": "5dfa07a63a20814f80d60d6d",
    "privilege": "admin",
    "username": "admin 1",
    "tagType": 
        "color": "red",
        "text": "I'm an admin",
        "dateAdded": "2019-12-18T11:04:06.461Z"
    ,
    "__v": 0

【讨论】:

感谢您的回答和解释!

以上是关于仅包含与另一条路径的值相关的 Mongoose 模式的路径的主要内容,如果未能解决你的问题,请参考以下文章

查找与另一列的值相关的一个值

仅返回一列中的日期与另一列中的日期最接近的行?

将一列与另一数据框列匹配并粘贴第二个数据中的值 - Python

pyspark 将列值与另一列进行比较包含值范围

根据与另一列的部分匹配创建新列

如何将同一表中的一条记录与另一条记录关联?