简体   繁体   中英

ExpressJS Routing with Two MongoDB Collections Query

I currently have a collection modeled that displays all of the documents that match a query of two properties within my model. These two properties are displayed within my url path and a resulting route is triggered by grabbing the two properties from the route and displaying documents that match those values. ex color: 'red', day:'monday', if a URL is displayed with /red/monday, then all of the documents (images) with those values are displayed. My current route performs this query correctly.

I now want to create a second collection that contains the descriptions for each of the possible combinations of 'color' and 'day'. Basically when I go to /red/monday, the images are displayed and then a description also appears.

I'm not sure the best way to join these two combinations within one query within a route. Any help?

Images model:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var imageSchema = new Schema({
    day: { type: String, enum: ['Monday', 'Tuesday', 'Wednesday'] },
    color: { type: String, enum: ['Black', 'Blue', 'White'] },
    imageName: String,
    imageUrl: String,
    imageSource: String
});

var Images = mongoose.model('Images', imageSchema);

module.exports = Images;

Descriptions model:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var descriptionSchema = new Schema({
    pattern: { type: String, enum: ['Monday', 'Tuesday', 'Wednesday'] },
    color: { type: String, enum: ['Black', 'Blue', 'White'] },
    body: String,
});

var Description = mongoose.model('Description', descriptionSchema);

module.exports = Description;

Current Routing:

var express = require('express');
var router  = express.Router();
var Images = require('./models/imagesModel');
var Description = require('./models/descriptionModel');

    /*==== Color Pages  ====*/

    router.get('/:color/day-selection', function(req, res){

        console.log(req.params.color);

        res.render('pages/color.hbs', {
            color: req.params.pattern
        });
    });


    /*====  Results ====*/


    router.get('/:color/:day/result', function(req, res){

        console.log(req.params.color);

        Images.find( { pattern: req.params.color, color: req.params.day }, function(err, image){
            if (err) { console.log(err); }
            console.log(image);

            res.render('pages/result.hbs', {
                color   : req.params.color,
                day : req.params.day,
                image : image
            });
        });
    });

I would try to make the urls a bit more RESTful.

Url to get all colors for a day would be something like

route.get(':days') => serves GET /mondays -> get a list of all colors for monday.

route.get('/days') => serves GET /days -> get a list of all days

route.get(':days/:colors') => serves GET /mondays/reds -> get description of red for monday.

route.get(':day/:color/images') => servers GET /mondays/reds/images -> get all images for /mondays/reds/images

route.get(':days/:colors/images/:id') => serves GET :days/:colors/images/:id => serves GET on /mondays/reds/images/mongo-db-id -> get that specific image

Couple of other changes,

  1. Move the service layer code out of routers and inject them into the instance of express() . Example, read shameless plug, - https://github.com/swarajgiri/express-bootstrap/blob/master/bootstrap/app.js#L86
  2. Pass db connections as params to the service layers. https://github.com/swarajgiri/express-bootstrap/blob/master/core/index.js
  3. (Optional) - Use promises and generators for flow control. Ex - https://gist.github.com/swarajgiri/16202e32aa4d80d45c62

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM