search

Home  >  Q&A  >  body text

node.js - mongodb如何定义嵌套文档。

作为一个mongodb的初学者,我想实现一个mongodb的嵌套查询,接下来是我的代码,各位大神看看是哪里出了问题。

var mongoose = require('mongoose'),
  Schema = mongoose.Schema;


/*
 Applicant Schema
 */
var interview = new Schema({
  date: {
    type: Date
  },
  interviewer: {
    type: String
  },
  comment: {
    type: String
  }


});

var applicantschema = new Schema({
  job: {
    type: String,
    required: true

  },
  name: {
    type: String

  },
  gender: {
    type: String
  },
  birth: {
    type: Date
  },
  email: {
    type: String
  },
  telephone: {
    type: String
  },
  arrivalDate: {
    type: Date
  },
  years: {
    type: Number
  },
  wage: {
    type: Number
  },
  remarks: {
    type: String
  },
  result: {
    type: String
  },
  interview: [interview]

});

module.exports = mongoose.model('applicant',applicantschema);

以上lib端的model文件,接下来就是app端的controller文件中的关于写入mongodb里面的部分代码。

  $scope.applicant.name = $scope.name;
      $scope.applicant.gender = $scope.gender;
      $scope.applicant.birth = $scope.birth;
      $scope.applicant.email = $scope.email;
      $scope.applicant.telephone = $scope.telephone;
      $scope.applicant.years = $scope.years;
      $scope.applicant.wage = $scope.wage;
      $scope.applicant.remarks = $scope.remarks;
      $scope.applicant.job = $scope.job;
      $scope.applicant.arrivalDate = $scope.arrivalDate;
      //$scope.applicant.result = $scope.result;
      $scope.applicant.interview.date = $scope.date;
      $scope.applicant.interview.interviewer = $scope.interviewer;
      $scope.applicant.interview.comment = $scope.comment;

      console.log( $scope.applicant.interview.date + "&&&&&&&&&&&");
      
      然后报错显示的是interview is  undefined.
伊谢尔伦伊谢尔伦2767 days ago616

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-17 14:25:34

    I have never had such a requirement, so I will help you google it. See if it’s right

    interview:{
    type: mongoose.Schema.Types.Object, ref: 'interview'
    }

    http://stackoverflow.com/questions/18001478/referencing-another-schema-in-mongoose

    reply
    0
  • Cancelreply