具有mongodb的节点js一次从不同的表中获取值

时间:2018-02-15 06:22:53

标签: node.js mongodb mongoose

我只是节点js和mongo db中的新手。我为事件数据采用了两种模型,一种是事件数据,另一种是事件包。所以事件的模型看起来像这样

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const Schema = mongoose.Schema;

const bcrypt = require('bcrypt-nodejs');

const EventSchema = new Schema({
  _id: mongoose.Schema.Types.ObjectId,
  eventname: {
    type: String,
    required : true,
  },
  eventdesc: {
    type: String,
    required : true,
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: {
    type: Date,
    default: Date.now
  }
});

module.exports = mongoose.model('Event', EventSchema);

并且包的模型看起来像这样

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const Schema = mongoose.Schema;

const bcrypt = require('bcrypt-nodejs');

const PackageSchema = new Schema({
  _id: mongoose.Schema.Types.ObjectId,
  eventpackages: {
    type: Object,
    required : false,
  },
  event: { 
    type: mongoose.Schema.Types.ObjectId, 
    ref: 'Event'
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: {
    type: Date,
    default: Date.now
  }
});

module.exports = mongoose.model('Package', PackageSchema);

这是我的事件路由器文件,我获取事件的列表和详细信息

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');

const Event = require('../../model/event.model');
const Package = require('../../model/package.model');


/* GET ALL EVENTS */
router.get('/', function(req, res, next) {
  Event.find(function (err, events) {
    if (err) return next(err);
    res.json(events);
  });
});

/* GET SINGLE EVENT BY ID */
router.get('/:id', function(req, res, next) {
  Event.findById(req.params.id, function (err, post) {
    if (err) return next(err);
      res.json(post);
  });

  // Package.find({'event': req.params.id}, function (err, post) {
  //   if (err) return next(err);
  //     res.json(post);
  // });
});

/* SAVE EVENT */

router.post('/', function(req, res, next)  {

let event =  new Event ({
          _id             : new mongoose.Types.ObjectId(),
          eventname       : req.body.eventname,
          eventdesc       : req.body.eventdesc        
      });
        event.save((err) => {
        if(err) {
          if( err.errors ) {
            if( err.errors.eventname ) {
              res.json({ success: false, message: err.errors.eventname.message });
            }
            else {
              if( err.errors.eventdesc ) {
                res.json({ success: false, message: err.errors.eventdesc.message });
              }
            }
          }
          else {
            res.json({ success: false, message: 'Could not save Event. Error: ', err });
          }
        }
        else {
          let package = new Package({
            _id: new mongoose.Types.ObjectId(),
            event: event._id,
            eventpackages  : req.body.sections,
          });

          package.save(function(err) {
            if (err) throw err;
            res.json({ success: true, message: 'Event saved'});
        });
        }
      });
    });

/* UPDATE EVENT */
router.put('/:id', function(req, res, next) {
  Event.findByIdAndUpdate(req.params.id, req.body, function (err, post) {
    if (err) return next(err);
    res.json(post);
  });
});

/* DELETE EVENT */
router.delete('/:id', function(req, res, next) {
  Event.findByIdAndRemove(req.params.id, req.body, function (err, post) {
    if (err) return next(err);
    res.json(post);
  });
});

module.exports = router;

现在,当我为事件制作列表或详细信息页面时,我希望获得事件的所有详细信息及其包已在包模式中作为引用ID。那么有人可以告诉我应该如何查询参考表,以便我一次获取所有数据并显示它们

1 个答案:

答案 0 :(得分:-1)

首先,你必须在事件结果中存储相应的package._id,而不是通过传递两个id来查询。