Note that there are some explanatory texts on larger screens.

plurals
  1. PORelating Mongodb Subdocuments
    text
    copied!<p>I'm building a REST api in node and i'm stuck on trying to include subdocuments into my GET requests. For example, I have two schema's right now, People and Locations. A location can have many people, and a person can have one location. So when you return /people I would like my location entry to contain the location information.</p> <p>I believe my problem could be one of two things. I'm fairly new to node, so i'm not 100% sure if my schema's can see each other, when I try the common methods around the web, my location field get's populated with null. How I understand it, I store the location id in my people schema and then using subdocuments it will find the location with that id and fill in the info.</p> <p>I also am not 100% sure how to use the populate function, and how exactly I should go about writing the response to my GET call. My code below, I'd love to hear what you have to say! </p> <p>app.js</p> <pre><code>// Node Setup var application_root = __dirname, express = require('express'), path = require('path'), mongoose = require('mongoose'), http = require('http'); var app = express(); // MongoDB Connection var dbLocalhost = mongoose.createConnection('mongodb://localhost/lantern/'); // Configure Node app.configure(function(){ app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(path.join(application_root, "public"))); app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); app.port = 3000; }); // Routes var Locations = require('./routes/locations')(app, { 'mongoose': mongoose, 'db': dbLocalhost }); var People = require('./routes/people')(app, { 'mongoose': mongoose, 'db': dbLocalhost }); // Start the server app.listen(app.port); </code></pre> <p>routes/people.js - has all the endpoints, but we're just concerned with GET for now</p> <pre><code>module.exports = function (app, options) { var mongoose = options.mongoose; var Schema = options.mongoose.Schema; var db = options.db; var PeopleModel = require('../schemas/peopleSchema')(db); app.get('/people', function (req, res) { return PeopleModel.find(function (err, obj) { if (!err) { return res.send(obj); } else { return res.send(err); } }); }); }; </code></pre> <p>schemas/peopleSchema.js (the "location" field is what i want to populate)</p> <pre><code>module.exports = function(db) { return db.model('People', PeopleSchema()); } function PeopleSchema () { var Schema = require('mongoose').Schema; return new Schema({ first_name: String, last_name: String, address: { unit: Number, address: String, zipcode: String, city: String, region: String, country: String }, image: String, job_title: String, created_at: { type: Date, default: Date.now }, active_until: { type: Date, default: null }, hourly_wage: Number, location: [{type: Schema.ObjectId , ref: 'Locations'}], // Inheirit store info employee_number: Number }, { collection: 'people' }); } </code></pre> <p>schemas/locationsSchema.js - and the locations schema just incase</p> <pre><code>module.exports = function(db) { return db.model('Locations', LocationsSchema()); } function LocationsSchema () { var Schema = require('mongoose').Schema; return new Schema({ title: String, address: { unit: Number, address: String, zipcode: String, city: String, region: String, country: String }, current_manager: String, // Inherit person details alternate_contact: String, // Inherit person details hours: { sunday: String, monday: String, tuesday: String, wednesday: String, thursday: String, friday: String, saturday: String, holidays: String }, employees: "", // mixin employees that work at this location created_at: { type: Date, default: Date.now }, active_until: { type: Date, default: null } }, { collection: 'locations' }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload