Skip to content

Instantly share code, notes, and snippets.

@prashanth-sams
Last active September 5, 2019 07:28
Show Gist options
  • Save prashanth-sams/9b77f8a5a635d25c11bc28f9c1843ebd to your computer and use it in GitHub Desktop.
Save prashanth-sams/9b77f8a5a635d25c11bc28f9c1843ebd to your computer and use it in GitHub Desktop.
Multiple model
"use strict";
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Define collection and schema for Manager
var Manager = new Schema({
tag_name: [{
type: String
}],
book_name: {
type: String
},
chapter_number: {
type: Number
},
verse_number: {
type: String
},
verse_context: {
type: String
}
},{
collection: 'manager'
});
// Define collection and schema for Question
var Question = new Schema({
query_text: {
type: String
},
tag_name: [{
type: String
}],
book_name: {
type: String
},
chapter_number: {
type: Number
},
verse_number: {
type: String
},
verse_context: {
type: String
}
},{
collection: 'manager'
});
const managerSchema = mongoose.model('Manager', Manager);
const questionSchema = mongoose.model('Question', Question);
module.exports = { Manager: managerSchema, Question: questionSchema }
var { Question } = require('./manager.model');
// Defined store route
managerRoutes.route('/question/create').post(function (req, res) {
let question = new Question(req.body);
question.save()
.then(question => {
res.status(200).json({'question': 'question is added successfully'});
})
.catch(err => {
res.status(400).send("unable to save to database");
});
});
// Filter with query_text
managerRoutes.route('/question/:query_text').get(function (req, res) {
let query_text = req.params.query_text;
Question.find({ query_text: query_text }, function(err, data){
res.json(data);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment