如何访问嵌套数据,它是猫鼬数组中的参考数据?

时间:2019-05-20 08:03:25

标签: node.js mongodb express mongoose

所以我有这个嵌套的数据对象,它也是一个参考数据。当我尝试使用填充访问它时,它仅显示ID。我只想要ID。我也希望细节出现。有人可以帮我吗?

employeesdetails

这是EmployeesDetailsS​​chema,其中具有此employeesinfo属性及其引用。现在,您可以在图像中看到的名称和特殊属性仅以id表示,而不是完整的详细信息,我想要这两个以及其他字段的完整详细信息。

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

const EmployeesDetailsSchema = new Schema({

  employeesinfo: {
    type: mongoose.Schema.Types.ObjectId, 
    ref: "employees" 
  },
  workingdays: Number,
  fathersname: String,
  pan: String,
  joiningdate: Date,
  gender: String,
  pfno: String,
  esino: String,
  branchname: String,
  department: String,
  paymode: String,
  bankname: String,
  acno: String,
  ifscno: String,

  //Earnings
  basicsalary: Number,
  extra: Number,
  totalE: Number,

  //Days
  fixeddays: Number,
  presentdays: Number,
  absentdays: Number,
  leavedays: Number,
  holidays: Number,

  //Deductions
  pf: Number,
  esi: Number,
  professionaltax: Number,
  advance: Number,
  absentdeductions: Number,
  leavedeductions: Number,
  totalD: Number,

  //Net pay Details
  netpay: Number,
  inwords: String,
  name: String,
  date: {
    type: Date,
    default: Date.now
  }
});

module.exports = EmpDetails = mongoose.model(
  "empdetails",
  EmployeesDetailsSchema
);

这是EmployeesSchema,它是EmployeesDetailsS​​chema的employeesinfo属性中的引用

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

const EmployeesSchema = new Schema({
  name: { type: String },
  email: { type: String },
  speciality: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "speciality"
  },
  contactno: { type: Number },
  designation: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "designation"
  },
  alternatecontactno: { type: Number },
  address: { type: String },
  employeeImage: { type: String },
  imageName: { type: String },
  date: { type: Date, default: Date.now() }
});

module.exports = Employees = mongoose.model("employees", EmployeesSchema);

这是EmployeesSchema中正在引用的两个模型

//Speciality Type Schema
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const SpecialitySchema = new Schema({
  speciality: {
    type: String
  },
  description: {
    type: String
  }
});
module.exports = Speciality = mongoose.model("speciality", SpecialitySchema);
//Designation Type Schema
const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const DesignationSchema = new Schema({
  designation: {
    type: String
  },
  description: {
    type: String
  }
});
module.exports = Designation = mongoose.model("designation", DesignationSchema);

这是获取路线

router.get("/", (req, res) => {


 EmpDet.find()
    .populate({
      path: "employeesinfo"
    })
    .then(empdet => res.json(empdet))
    .catch(err =>
      res.status(400).json({ msg: "Error in finding Employees Details" })
    );
});

2 个答案:

答案 0 :(得分:0)

如果未设置特定选项,Populate通常会返回整个参考文档。可以这样使用: ['AAA1','BBB1', "CCC1", 'AAA2','BBB2', "CCC2",...,'AAA100','BBB100', "CCC100"]

查看完整的文档和更多示例: https://mongoosejs.com/docs/populate.html

答案 1 :(得分:0)

尝试一下:

EmpDet.find()
    .populate({
      path: "employeesinfo",
      populate: [
         { path: 'speciality' },
         { path: 'designation' }
      ]

    })
    .then(empdet => res.json(empdet))
    .catch(err =>
      res.status(400).json({ msg: "Error in finding Employees Details" })
    );

此外,也请参考Simon提到的文档。 https://mongoosejs.com/docs/populate.html