schema.js
var mongoose = require('mongoose');
var assert = require('assert');
var schema = mongoose.Schema;
var tutorialSchema = new schema({
topic:{
type:String,
require:true,
unique:true },
description:{
type:String,
require:true }
});
var Tutorials = mongoose.model('tutorials', tutorialSchema);
module.exports = Tutorials;
app.js
var mongoose = require('mongoose');
var assert = require('assert');
var Tutorials = require('./schema.js');
var url = 'mongodb://localhost:27017/MyDb';
mongoose.connect(url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'Connection error'));
db.on('open', function () {
console.log('Connected');
});
var NewTopic = Tutorials({
topic : 'Express.js',
description : 'Express Framework',
});
NewTopic.save(function (err) {
if(err) throw err;
Tutorials.find(function (err, data) {
if(err) throw err;
console.log(data);
});
});