简体   繁体   English

mongoose 中的嵌套模式

[英]Nesting schema in mongoose

Trying to create nested products in mongoose schema.尝试在 mongoose 架构中创建嵌套产品。 And trying to create schema into schema.并尝试将架构创建为架构。 How to make that, and how after that to get the nested schema?如何做到这一点,以及之后如何获得嵌套模式?

reffering to mongoose document you can do it like so:参考mongoose 文档,您可以这样做:

  const schema = new Schema({
  name:    String,
  binary:  Buffer,
  living:  Boolean,
  updated: { type: Date, default: Date.now },
  age:     { type: Number, min: 18, max: 65 },
  mixed:   Schema.Types.Mixed,
  _someId: Schema.Types.ObjectId,
  decimal: Schema.Types.Decimal128,
  array: [],
  ofString: [String],
  ofNumber: [Number],
  ofDates: [Date],
  ofBuffer: [Buffer],
  ofBoolean: [Boolean],
  ofMixed: [Schema.Types.Mixed],
  ofObjectId: [Schema.Types.ObjectId],
  ofArrays: [[]],
  ofArrayOfNumbers: [[Number]],
  nested: {
    stuff: { type: String, lowercase: true, trim: true }
  },
  map: Map,
  mapOfString: {
    type: Map,
    of: String
  }
})

for example for an object of strings you can use the nested property.例如,对于字符串的 object,您可以使用嵌套属性。

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

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