简体   繁体   English

猫鼬模式中嵌套对象中的元素需求如何

[英]How require elements in nested object in Mongoose Schema

Creating a Schema I need some nested objects to be always in the data post to be saved in my DB. 创建模式我需要一些嵌套对象始终位于要保存在数据库中的数据发布中。

I tried this way creating 2 schemas and the object valid is being required but I need also valid.from and valid.to to be required and this way is not working. 我想这种方式创建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 }
});

But what I was able to try, your example works. 但是我能够尝试的,您的示例有效。 I used following schemas: 我使用以下模式:

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,
  },
});

And by using this schema when I tried to save anything else than: 当我尝试保存除以下内容以外的其他内容时,请使用此架构:

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

the save failed. 保存失败。

Example: If i tried saving: 示例:如果我尝试保存:

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

the code failed with: 代码失败,原因:

[Nest] 8895 - 06/12/2019, 1:42 PM [ExceptionsHandler] Cat validation failed: valid.from: Path from is required., valid: Validation failed: from: Path from is required. [嵌套] 8895-2019年12月12日,下午1:42 [ExceptionsHandler] Cat验证失败:valid.from:需要from路径from 。,有效:验证失败:from:需要从路径from +829ms [0] ValidationError: Cat validation failed: valid.from: Path from is required., valid: Validation failed: from: Path from is required. + 829ms [0] ValidationError:猫验证失败:valid.from:路径from 。是必需的,有效的:验证失败:从:路径from是必需的。 [0] at new ValidationError (/home/adrian/work/try/node_modules/mongoose/lib/error/validation.js:30:11) [0] at model.Document.invalidate (/home/adrian/work/try/node_modules/mongoose/lib/document.js:2260:32) [0] at SingleNested.Subdocument.invalidate (/home/adrian/work/try/node_modules/mongoose/lib/types/subdocument.js:147:18) [0] at p.doValidate.skipSchemaValidators (/home/adrian/work/try/node_modules/mongoose/lib/document.js:2109:17) [0] at /home/adrian/work/try/node_modules/mongoose/lib/schematype.js:981:9 [0] at processTicksAndRejections (internal/process/task_queues.js:82:9) [0]在新的ValidationError(/home/adrian/work/try/node_modules/mongoose/lib/error/validation.js:30:11)在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(内部/进程/task_queues.js:82:9)

And to finish, i was using following dependencies: 最后,我正在使用以下依赖项:

{
  "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"
  },
}

You need to write your LicenseSchema this way: 您需要以这种方式编写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
  }
});

By the way, this might be a possible duplicate of Reference in mongoose schema 顺便说一下,这可能是猫鼬模式Reference的可能副本

Let me know if it helps 让我知道是否有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM