如何使用猫鼬访问嵌套的对象数组?

时间:2019-05-20 09:53:18

标签: node.js mongodb express mongoose

所以我有一个嵌套在一个集合中的对象数组。我主要关心的是,当我访问这些嵌套对象时,它仅显示id。我想要这些数据的完整细节。我该怎么做?

这是用于产品注册的proreg模型

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

// Create Schema
const ProRegSchema = new Schema({

  refno1: {
    type: String
  },
  refno2: {
    type: String
  },
  date: {
    type: Date
  },
  customer: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customer" 
},
customertype: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customertype" 
},
department: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "customersubdepartment" 
},

  products: [
    {
      oem: {
        type: Schema.Types.ObjectId,
        ref: 'product'
      },
      category: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "product"
      },
      subcategory: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "product"
      },
      modelno: {
        type: String
      },
      serialno: {
        type: String,
        required: true
      },
      warrantyfrom: {
        type: Date,
        default: "01-01-2019"
      },
      warrantyto: {
        type: Date,
        default: "01-01-2019"
      },
      oemwarrantyfrom: {
        type: Date,
        default: "01-01-2019"
      },
      oemwarrantyto: {
        type: Date,
        default: "01-01-2019"
      }, 
    }
  ],
  date: {
    type: Date,
    default: Date.now
  }
});

module.exports = ProReg = mongoose.model('proreg', ProRegSchema);

这是proreg模型中正在引用的产品模型

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

//Products Schema
const ProductSchema = new Schema({
  oem: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "company"
  },
  category: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "productcategory"
  },
  subcategory: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "productsubcategory"
  },
  modelno: { type: String },
  description: { type: String },
  specification: { type: String },
  productdoc: {type: String},
  date: {
    type: Date,
    default: Date.now
  }
});
module.exports = Product = mongoose.model("product", ProductSchema);

This is the get routes

    router.get('/:id', (req, res) => {
    	ProReg.findById(req.params.id)
    		.populate('customer', 'customername')
    		.populate('customertype', 'customertype')
    		.populate('department', 'department')
    		.populate('products.$.oem.$.companyname')
    		.then((proreg) => res.json(proreg))
    		.catch((err) => res.status(404).json({ msg: 'No Pro Reg found with that ID' }));
    });

这些也是嵌套在产品模型中的模型

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

//Company Schema
const CompanySchema = new Schema({
  companyname: {
    type: String
  },
  contactperson: {
    type: String
  },
  contactno: {
    type: Number
  },
  alternatecontactno: {
    type: Number
  },
  email: {
    type: String
  },
  fax: {
    type: Number
  },
  address: {
    type: String
  },
  date: {
    type: Date,
    default: Date.now
  }
});
module.exports = Company = mongoose.model("company", CompanySchema);

//Product Category Schema
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const ProductCategorySchema = new Schema({
  category: {
    type: String
  },
  description: {
    type: String
  }
});
module.exports = ProductCategory = mongoose.model(
  "productcategory",
  ProductCategorySchema
);

//Product Sub Category Schema
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const ProductSubCategorySchema = new Schema({
  subcategory: {
    type: String
  },
  parentcategory: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "productcategory"
  },
  description: {
    type: String
  }
});
module.exports = ProductSubCategory = mongoose.model(
  "productsubcategory",
  ProductSubCategorySchema
);

这是我得到的回应 proreg

0 个答案:

没有答案