猫鼬嵌套数组填充

时间:2019-02-13 14:21:29

标签: node.js mongodb mongoose mongoose-populate

我有一个集合名称vehicle。我将vehicle_type存储在vehicle模式中。我没有任何vehicle_type收藏。

我创建了另一个集合名称driving_licence。在此集合中,我存储了vehiclevehicle_type ref,并且我想填充这些vehiclevehicle_type的细节。

车辆架构

  var mongoose = require("mongoose"),
  bcrypt = require("bcryptjs"),
  Schema = mongoose.Schema;

var TypeSchema = new Schema({
  title: { type: String, required: true },
  status: { type: Boolean, default: true }
});

/**
 * Vehicle Schema
 */
var VehicleSchema = new Schema(
  {
    title: {
      type: String,
      trim: true,
      required: true
    },
    type: [TypeSchema],
    status: { type: Boolean, default: true }
  },
  {
    timestamps: {
      createdAt: "created",
      updatedAt: "updated"
    },
    id: false,
    toJSON: {
      getters: true,
      virtuals: true
    },
    toObject: {
      getters: true,
      virtuals: true
    }
  }
);
VehicleSchema.index({ title: 1 });
module.exports.Vehicle = mongoose.model("Vehicle", VehicleSchema);
module.exports.Type = mongoose.model("Type", TypeSchema);

驾驶执照架构

var mongoose = require("mongoose"),
  bcrypt = require("bcryptjs"),
  Schema = mongoose.Schema;
  const Type = require("../models/Vehicle.js").Type;
/**
 * Vehicle Schema
 */
var DrivingLicenceSchema = new Schema(
  {
    title: {
      type: String,
      trim: true,
      required: true
    },
    licence_type: [
      {
        vehicle: { type: Schema.Types.ObjectId, ref: "Vehicle" },
        vehicle_type: [
          {
            _id: { type: Schema.Types.ObjectId, ref: "Type" },
            status: { type: Boolean, default: true }
          }
        ]
      }
    ],
    status: { type: Boolean, default: true }
  },
  {
    timestamps: {
      createdAt: "created",
      updatedAt: "updated"
    },
    id: false,
    toJSON: {
      getters: true,
      virtuals: true
    },
    toObject: {
      getters: true,
      virtuals: true
    }
  }
);
DrivingLicenceSchema.index({ title: 1 });
module.exports.DrivingLicence = mongoose.model(
  "DrivingLicence",
  DrivingLicenceSchema
);

请帮助我。

0 个答案:

没有答案