javascript - Populating one-to-many relationships with MongoDB in MEAN Stack -
i followed article in mongodb docs , have created similar one-to-many relationship, referencing "publisher" id
value in children. data structure follows, publisher_id
being working piece here:
{ _id: 123456789, title: "mongodb: definitive guide", author: [ "kristina chodorow", "mike dirolf" ], published_date: isodate("2010-09-24"), pages: 216, language: "english", publisher_id: "oreilly" } { _id: 234567890, title: "50 tips , tricks mongodb developer", author: "kristina chodorow", published_date: isodate("2011-05-06"), pages: 68, language: "english", publisher_id: "oreilly" }
if have page, example, all o'reilly books, how can return objects proper publisher_id
. typically done on front-end or back-end?
yes, typically query mongo database on end (node.js) using mongoose middleware. can use mongodb plugin. can send query angular front end http call, node routes server side controller. server side book.controller file this:
var mongoose = require('mongoose'), books = mongoose.model('books') exports.findbooks = function(req,res){ var query = {publisher_id:req.body.publishername}; books.find(query,function(err,bookresults){ if (err){ return res.status(400).send({message: err}) } else { res.status(200).send({books: bookresults}); } }) };
your node.js route this:
var controller = require('./controllers/book.controller.js'), express = require('express'), app = express(), router = express.router(); router.post('/find/books',controller.findbooks);
your angular front end file make call this:
$http.post('/find/books',{publishername: $scope.publisher}) .success(function(res){ //handle data received })
this answer basic query of data, assumes user know publisher id on front end. query list of publisher ids , names on page load , let them choose drop down, run query. if looking joins, sql joining of 2 tables, @ answer: mongoose/mongodb query joins.. come sql background
hope helps.
Comments
Post a Comment