[英]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.
这两个属性显示在我的url路径中,并通过从路由中获取两个属性并显示与这些值匹配的文档来触发生成的路由。 ex color: 'red', day:'monday', if a URL is displayed with /red/monday, then all of the documents (images) with those values are displayed.
例如:“红色”,“日期”:“星期一”,如果显示的网址带有/ red / monday,则显示所有具有这些值的文档(图像)。 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.
基本上,当我转到/ red / monday时,将显示图像,然后还会显示说明。
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更加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')
=>服务GET /mondays
>获取星期一所有颜色的列表。
route.get('/days')
=> serves GET /days
-> get a list of all days route.get('/days')
=>服务GET /days
route.get('/days')
>获取所有日期的列表
route.get(':days/:colors')
=> serves GET /mondays/reds
-> get description of red for monday. route.get(':days/:colors')
=>服务GET /mondays/reds
>获取星期一的红色描述。
route.get(':day/:color/images')
=> servers GET /mondays/reds/images
-> get all images for /mondays/reds/images
route.get(':day/:color/images')
=>服务器获取/mondays/reds/images
>获取/mondays/reds/images
所有/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 route.get(':days/:colors/images/:id')
=>服务GET :days/:colors/images/:id
=>服务于/mondays/reds/images/mongo-db-id
>得到那个特定的图像
Couple of other changes, 其他一些变化,
express()
. express()
实例中。 Example, read shameless plug, - https://github.com/swarajgiri/express-bootstrap/blob/master/bootstrap/app.js#L86
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.