如何在 Mongoose Schema 中的嵌套对象中需要元素

Posted

技术标签:

【中文标题】如何在 Mongoose Schema 中的嵌套对象中需要元素【英文标题】:How require elements in nested object in Mongoose Schema 【发布时间】:2019-10-22 21:00:50 【问题描述】:

创建架构我需要一些嵌套对象始终在数据帖子中以保存在我的数据库中。

我尝试以这种方式创建 2 个模式,并且需要对象 valid,但我还需要 valid.fromvalid.to,但这种方式不起作用。

import * as mongoose from 'mongoose';

const LicenseTime = new mongoose.Schema(
  from:  type: Date, required: true ,
  to:  type: Date, required: true ,
);

export const LicenseSchema = new mongoose.Schema(
  customer:  type: String, required: true ,
  appId:  type: String, required: true ,
  purchaseDate:  type: Date, required: true ,
  valid:  type: LicenseTime, required: true 
);

【问题讨论】:

【参考方案1】:

但是我能够尝试,您的示例有效。我使用了以下模式:

import  Schema  from 'mongoose';

const LicenseTime = new Schema(
  from: 
    type: Date,
    required: true,
  ,
  to: 
    type: Date,
    required: true,
  ,
,

  _id: false, // Used so mongoose/mongo doesn't create id
  id: false, // Used so mongoose/mongo doesn't create id
);

export const LicenseSchema = new Schema(
  customer: 
    type: String,
    required: true,
  ,
  appId: 
    type: String,
    required: true,
  ,
  purchaseDate: 
    type: Date,
    required: true,
  ,
  valid: 
    type: LicenseTime,
    required: true,
  ,
);

当我尝试保存其他内容时使用此架构:


  customer: 'example customer',
  appId: 'example appId',
  purchaseDate: new Date(),
  valid: 
    to: new Date(),
    from: new Date(),
  ,

保存失败。

示例:如果我尝试保存:


  customer: 'example customer',
  appId: 'example appId',
  purchaseDate: new Date(),
  valid: 
    to: new Date(),
  ,

代码失败:

[Nest] 8895 - 2019 年 6 月 12 日下午 1:42 [ExceptionsHandler] Cat 验证失败:有效。来自:需要路径 from。有效:验证失败:来自:需要路径 from . +829 毫秒 [0] ValidationError:Cat 验证失败:valid.from:路径from 是必需的。有效:验证失败:来自:路径from 是必需的。 [0] 在新的 ValidationError (/home/adrian/work/try/node_modules/mongoose/lib/error/validation.js:30:11) [0] 在 model.Document.invalidate (/home/adrian/work/try/node_modules/mongoose/lib/document.js:2260:32) [0] 在 SingleNested.Subdocument.invalidate (/home/adrian/work/try/node_modules/mongoose/lib/types/subdocument.js:147:18) [0] 在 p.doValidate.skipSchemaValidators (/home/adrian/work/try/node_modules/mongoose/lib/document.js:2109:17) [0] 在 /home/adrian/work/try/node_modules/mongoose/lib/schematype.js:981:9 [0] 在 processTicksAndRejections (internal/process/task_queues.js:82:9)

最后,我使用了以下依赖项:


  "dependencies": 
    "@nestjs/common": "^6.0.0",
    "@nestjs/core": "^6.0.0",
    "@nestjs/mongoose": "^6.1.2",
    "@nestjs/platform-express": "^6.0.0",
    "mongoose": "^5.5.14",
    "reflect-metadata": "^0.1.12",
    "rimraf": "^2.6.2",
    "rxjs": "^6.3.3"
  ,
  "devDependencies": 
    "@nestjs/testing": "^6.0.0",
    "@types/express": "^4.16.0",
    "@types/jest": "^23.3.13",
    "@types/mongoose": "^5.5.6",
    "@types/node": "^10.12.18",
    "@types/supertest": "^2.0.7",
    "concurrently": "^4.1.0",
    "jest": "^23.6.0",
    "nodemon": "^1.18.9",
    "prettier": "^1.15.3",
    "supertest": "^3.4.1",
    "ts-jest": "24.0.2",
    "ts-node": "8.1.0",
    "tsconfig-paths": "3.8.0",
    "tslint": "5.16.0",
    "typescript": "3.4.3",
    "wait-on": "^3.2.0"
  ,

【讨论】:

【参考方案2】:

你需要这样写你的LicenseSchema

export const LicenseSchema = new mongoose.Schema(
  customer:  type: String, required: true ,
  appId:  type: String, required: true ,
  purchaseDate:  type: Date, required: true ,
  valid: 
    type: mongoose.Schema.Types.ObjectId,
    ref: "LicenseTime", 
    required: true
  
);

顺便说一句,这可能与Reference in mongoose schema 重复

如果有帮助请告诉我

【讨论】:

我猜这将创建 LicenseTime 并将其保存在另一个集合中

以上是关于如何在 Mongoose Schema 中的嵌套对象中需要元素的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 mongoose 和 nodejs 删除嵌套在 Post 模式中的评论?

mongoose 怎么插入嵌套数组

Mongoose 嵌套对象数组中的唯一值

Mongoose 嵌套对象数组中的唯一值

如何使用来自@nestjs/mongoose 的@Prop 装饰器添加嵌套的对象数组

如何使用 Node.js 和 Mongoose 将对象添加到嵌套数组