簡體   English   中英

貓鼬模式中嵌套對象中的元素需求如何

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

創建模式我需要一些嵌套對象始終位於要保存在數據庫中的數據發布中。

我想這種方式創建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 }
});

但是我能夠嘗試的,您的示例有效。 我使用以下模式:

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

代碼失敗,原因:

[嵌套] 8895-2019年12月12日,下午1:42 [ExceptionsHandler] Cat驗證失敗:valid.from:需要from路徑from 。,有效:驗證失敗:from:需要從路徑from + 829ms [0] ValidationError:貓驗證失敗:valid.from:路徑from 。是必需的,有效的:驗證失敗:從:路徑from是必需的。 [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)

最后,我正在使用以下依賴項:

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

您需要以這種方式編寫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的可能副本

讓我知道是否有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM