node.js - mongodb如何定义嵌套文档。
伊谢尔伦
伊谢尔伦 2017-04-17 14:23:34
0
1
624

作为一个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.
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回覆(1)
刘奇

沒做過這樣的需求,幫你google了一下。你看看對不對

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

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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板