Ekspor skema sub-dokumen parameter sebagai modul.
// Parameter Model file 'Parameter.js'
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ParameterSchema = new Schema({
id: {
type: String,
trim: true,
default: ''
},
value: {
type: String,
trim: true,
default: ''
}
});
module.exports = ParameterSchema;
// Not as a mongoose model i.e.
// module.exports = mongoose.model('Parameter', ParameterSchema);
Sekarang memerlukan skema modul yang diekspor di dokumen induk Anda.
// Require the model exported in the Parameter.js file
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Parameter = require('./Parameter');
var FirstCollectionSchema = new Schema({
name: {
type: String,
trim: true,
default: '
},
parameters: [Parameter]
});
module.exports = mongoose.model('FirstCollection', FirstCollectionSchema);
Sekarang Anda menyimpan koleksi dan sub dokumen.
var FirstCollection = require('./FirstCollection')
var feat = new FirstCollection({
name: 'foo',
parameters: [{
id: 'bar',
value: 'foobar'
}]
});
feat.save(function(err) {
console.log('Feature Saved');
})