How to use schemas in schemas when upgrading to Mongoose 4.x from Mongoose 3.x?

I have 2 files:

keyFeature:

mongoose = require('mongoose');

Schema = mongoose.Schema;

DescriptionSchema = require('./description');

KeyFeatureSchema = new Schema({
  descriptions: [DescriptionSchema],
  title: String,
  key: String,
  display: String,
  feature: {
    type: Schema.Types.ObjectId,
    ref: 'Module'
  }
});

module.exports = KeyFeatureSchema;

Description:

mongoose = require('mongoose');

DescriptionSchema = new mongoose.Schema();

DescriptionSchema.add({
  display: String,
  subdescriptions: [String]
});

module.exports = DescriptionSchema;

How can I rewrite this for compatibility with Mongoose 4 syntax .add?

+4
source share
1 answer

I'm not sure if I understood your question, but here is what I get.

mongoose.add documentation 4.5.2 . test.js . keyFeature . mongod mongo keyfeatures. db.

var mongoose = require('mongoose');
var DescriptionSchema = require('./description.js');
var KeyFeatureSchema = require('./keyFeature.js');

mongoose.connect('mongodb://localhost/mongoose-testing', function(err)    {
    if(err){
        console.log('Could not connect to mongoose', err);
    }else{
        console.log('Successfully connected to mongoose');
        mongoose.model('Description', DescriptionSchema);
        mongoose.model('Keyfeature', KeyFeatureSchema);
        var Keyfeature = mongoose.model('Keyfeature');

        new Keyfeature({
            descriptions: [{
                display: 'This is the display content from descriptions',
                subdescriptions: ['Subdescription 1']
            }],
            title: 'tittle',
            key: 'key',
            display: 'display'
        }).save(function(err){
            if(err){
                console.log('Error when saving', err);
            }else{
                console.log('Saved');
            }
        });
    }
});

mongoose@4.5.2

+1

All Articles