搜索

首页  >  问答  >  正文

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.
伊谢尔伦伊谢尔伦2769 天前627

全部回复(1)我来回复

  • 怪我咯

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

    没做过这样的需求,帮你google了下。你看看对不对

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

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

    回复
    0
  • 取消回复