Hey can you check if this helps SubjectBooks
is mongoose schema. The query for appending new books into the subject by subjectName. You can change first parameter of update method if you want to find objects with any other parameter like _id.
Node code goes here
EDITED
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
/*....mongoose connection creation code goes here...*/
var bookSchema=new Schema({subject_name:{type:String},books[{book_name:String, book_author:String}]
});
var SubjectBooks = mongoose.model('<collection_name>', bookSchema);
app.post('<URL mapping goes here>',function(req,res){
var subject = req.body; //You have to use body-parser
var subjectName = subject.subject_name;
var subjectModel = new SubjectBooks(subject);
subjectModel.save(function (err,mongRes) {
if(err){
res.send(err);
}
res.json(mongRes);
});
});
Angular code that I have created
app.controller("mainController",["$scope",'mainService',function($scope,mainService){
$scope.subjectBookCatalogue = {
subject_name: "",
books:[{
book_name:"",
book_author:""
}]
};
$scope.saveSubjects = function (data) {
mainService.saveSubjects(data).then(function(response){
console.log(response.data);
},function(response){
});
};
$scope.subjects = [];
$scope.fetchSubjects = function () {
mainService.getAllSubjects().then(function(response){
console.log(response.data);
$scope.subjects = response.data;
},function(response){
});
};
$scope.fetchSubjects();
$scope.submitForm = function(){
console.log($scope.subjectBookCatalogue);
$scope.saveSubjects($scope.subjectBookCatalogue);
}
}]);
app.factory("mainService",["$http",function($http){
return {
saveSubjects : function(data){
return $http({
method:"POST",
url: "http://localhost:3000/v1/subject/"+data.subject_name,
data:data,
headers: {
'Content-Type': 'application/json'
}
});
},
getAllSubjects : function(){
return $http({
method:"GET",
url:"http://localhost:3000/v1/subjects"
});
}
}
}]);
This should help for pushing books into a subject books array.
You can refer this question