如何使用NodeJS在模型类中使用嵌套数据

时间:2018-07-24 06:34:57

标签: node.js mongodb

我在MongoDB中有ug.tutionfees和pg.tutionfees之类的数据。如何在NodeJS模型类中实现这些数据以及angular plz告诉我。

我的模型班:

const mongoose = require('mongoose');

const UniversitySchema = mongoose.Schema({
    worldranking:String,
    countryranking:String,
    universityname:String,
    bachelorprogram:String,
    masterprogram:String,
    phdprogram:String,
    country:String
},{collection:'us'});

const University =module.exports = mongoose.model('University',UniversitySchema);

我想在此模型类中实现ug.tutionfeespg.tutionfees

1 个答案:

答案 0 :(得分:0)

如果只有ug.tutionfeespg.tutionfees这两个不同的对象,则可以将其添加到架构中,如下所示:

ug :{
    tutionfees :{
        type: String
    }
},
pg :{
    tutionfees :{
        type: String
    }
}

检查我的架构结构之一:

'use strict'

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

var rbpiSchema = new Schema({
  rbpiId: {
      type: String,
      required: true,
      unique: true
  },
  name: {
    type: String,
    required: true
  },
  description: {
    type: String
  },
  last_action: {
    activity: {
        type: String
    },
    status: {
        type: String
    },
    ut: {
        type: Number
    },
    description: {
        type: String
    }
  },
  ct: {
    type: Number,
    require: true,
    default: new Date().getTime()
  },
  ut: {
    type: Number,
    require: true,
    default: new Date().getTime()
  }
});

module.exports = mongoose.model('rbpi', rbpiSchema);