Mongodb Mongoose从不同集合中的数组向模式动态添加对象

Mongodb Mongoose从不同集合中的数组向模式动态添加对象,mongodb,mongoose,Mongodb,Mongoose,我有两个模式和模型 permission.ts import mongoose, { Document } from 'mongoose'; export interface Permission { name: string; displayName: { [key: string]: string; }; createdBy: string; updatedAt?: Date; updatedBy?: string; } const PermissionS

我有两个模式和模型

permission.ts

import mongoose, { Document } from 'mongoose';

export interface Permission {
  name: string;
  displayName: {
    [key: string]: string;
  };
  createdBy: string;
  updatedAt?: Date;
  updatedBy?: string;
}

const PermissionSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
      unique: true,
    },
    displayName: {
      type: Map,
      of: String,
    },
    createdBy: {
      type: mongoose.Schema.Types.Mixed,
      ref: 'User',
      required: true,
    },
    updatedBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      default: null,
    },
  },
  { timestamps: true }
);

export const PermissionModel = mongoose.model<Permission & Document>(
  'Permission',
  PermissionSchema
);
export default PermissionModel;
import mongoose, { Document } from 'mongoose';

import IRole from 'adventure.center.shared/interface/role.js';
import mongooseAutoPopulate from 'mongoose-autopopulate';
import { AutoPopulateSchemaOptions } from '@/utils';

const RoleSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
      unique: true,
    },
    permissions: [
      <AutoPopulateSchemaOptions>{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Permission',
        required: true,
        autopopulate: true,
      },
    ],
    createdBy: {
      type: mongoose.Schema.Types.Mixed,
      ref: 'User',
      required: true,
    },
    updatedBy: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
      default: null,
    },
  },
  { timestamps: true }
);
RoleSchema.plugin(mongooseAutoPopulate);

export const RoleModel = mongoose.model<IRole & Document>('Role', RoleSchema);
export default RoleModel;
我正在使用插件自动填充角色模型中的权限。例如:

{
  "_id": "605cd54c2a6460413009274b",
  "name": "Administrator",
  "createdBy": "6054f9fd0084012950fa73e3",
  "createdAt": "2021-03-25T18:24:12.615Z",
  "updatedAt": "2021-03-25T18:24:12.615Z",
  "updatedBy": null,
  "permissions": [
    {
      "_id": "600b129420f93cb18825483a",
      "updatedBy": null,
      "name": "EVENT.VIEW.ALL",
      "displayName": {
        "en": "View All Events"
      },
      "createdBy": "System"
    },
  ],
}
{
  "_id": "5fb7ed28197f5655a4bdb697",
  "name": "EDIT.OWN.USER",
  "displayName": {
    "en": "Edit Own User"
  },
  "roles": [{
    "_id": "605cd54c2a6460413009274b",
    "name": "Administrator",
    "createdBy": "6054f9fd0084012950fa73e3",
    "createdAt": "2021-03-25T18:24:12.615Z",
    "updatedAt": "2021-03-25T18:24:12.615Z",
    "updatedBy": null,
  }]
  "updatedBy": null,
  "createdBy": "System"
}
我的问题是,我想自动填充权限模型及其关联的角色,而不必将角色ID直接插入权限模型。例如:

{
  "_id": "605cd54c2a6460413009274b",
  "name": "Administrator",
  "createdBy": "6054f9fd0084012950fa73e3",
  "createdAt": "2021-03-25T18:24:12.615Z",
  "updatedAt": "2021-03-25T18:24:12.615Z",
  "updatedBy": null,
  "permissions": [
    {
      "_id": "600b129420f93cb18825483a",
      "updatedBy": null,
      "name": "EVENT.VIEW.ALL",
      "displayName": {
        "en": "View All Events"
      },
      "createdBy": "System"
    },
  ],
}
{
  "_id": "5fb7ed28197f5655a4bdb697",
  "name": "EDIT.OWN.USER",
  "displayName": {
    "en": "Edit Own User"
  },
  "roles": [{
    "_id": "605cd54c2a6460413009274b",
    "name": "Administrator",
    "createdBy": "6054f9fd0084012950fa73e3",
    "createdAt": "2021-03-25T18:24:12.615Z",
    "updatedAt": "2021-03-25T18:24:12.615Z",
    "updatedBy": null,
  }]
  "updatedBy": null,
  "createdBy": "System"
}
现在我可以通过编程实现这一点,但我很好奇mongoose/mongodb或是否有一些内置的内部功能来实现这类功能