关于Mongodb子文档

时间:2013-09-15 20:14:27

标签: javascript node.js mongodb express mongoose

我正在构建一个节点中的REST api,并且我一直试图将子文档包含到我的GET请求中。例如,我现在有两个架构,人员和位置。一个位置可以有很多人,一个人可以有一个位置。因此,当您返回/人员时,我希望我的位置条目包含位置信息。

我相信我的问题可能是两件事之一。我对节点很新,所以我不能100%确定我的架构是否可以看到对方,当我在网上尝试常用方法时,我的位置字段get填充了null。我是如何理解的,我将位置ID存储在我的人员模式中,然后使用子文档,它将找到具有该ID的位置并填写信息。

我也不是100%确定如何使用populate函数,以及我应该如何将响应写入我的GET调用。我的代码如下,我很想听听你的意见!

app.js

// 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);

routes / people.js - 拥有所有端点,但我们现在只关心GET

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);
          }
        });   
    });
};

schemas / peopleSchema.js(“location”字段是我想要填充的内容)

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' });


}

schemas / locationsSchema.js - 以及位置架构只是

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' });
}

0 个答案:

没有答案