与引用定义的对象相比,在MongoDB中导出模型有什么好处?

时间:2018-12-19 19:29:52

标签: mongodb mongoose mongoose-schema

我的问题是我如何在代码的后半部分导出两者。将两个对象都包含在Schema中然后作为VendorSchema导出,这两个对象之间有什么好处?

将接触点模型注册到Mongo是否有好处?

const VendorSchema = new Schema(
  {
    name: {
      type: String,
      unique: true
    },
    contacts: [PointOfContact.schema],
    purchaseOrders: [purchaseOrderRef]
  },
  { collection: "vendors" }
);

/// -------- vendor.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const PointOfContactSchema = new Schema({
  name: {
    type: String
  },
  emailAddress: {
    type: String
  },
  phoneNumber: {
    type: String
  }
});

module.exports = mongoose.model("pointofcontact", PointOfContactSchema);

// ------ refs.js

const mongoose = require("mongoose");

purchaseOrderRef = {
  type: mongoose.Schema.Types.ObjectId,
  ref: "purchaseOrder"
};

module.exports = purchaseOrderRef;

0 个答案:

没有答案