I am building a node and mongo / mongoose application that allows me to import and then 'query' bank statements via URL patterns.
My Statement model looks like
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var statementSchema = new Schema({
date : {type:Date, required:true},
name: {type:String, required:true},
method: {type:String},
amount: {type:Number, required:true},
category: {type:String, default:'Not Set'},
importDate: {type : Date, default:Date.now, required:true}
});
statementSchema.index({"date":1, "name":1, "amount":1}, {unique: true});
module.exports = mongoose.model('Statement', statementSchema);
and when I run a mongo query like db.statements.find({"name":"RINGGO"})
I get a number of documents returned that look like:
{
"_id" : ObjectId("5907a2850780a44f671707f5"),
"amount" : -6.3,
"name" : "RINGGO",
"method" : "VIS",
"date" : ISODate("2017-03-23T00:00:00Z"),
"importDate" : ISODate("2017-05-01T21:03:01.770Z"),
"category" : "Not Set",
"__v" : 0
}
I want to build the application so that:
- domain.com/:vendor returns all transactions at that vendor and the total spend
- domain.com/:vendor/2017 returns all transactions at that vendor in 2017 and the total 2017 spend
- domain.com/:vendor/2017/January returns all transactions at that vendor in January 2017 and the total Jan 2017 spend
- domain.com/:vendor/2017/January/wk01 returns all transactions at that vendor in wk 01 of January 2017 and the total wk 01 Jan 2017 spend
Currently I have a route (which passes data to Handlebars) that gets all spend (but not the total)
router.get('/:vendor?', function(req, res, next) {
var vendorName = req.params.vendor;
Statement.find({name:req.params.vendor}, function(err, doc){
if (err) {
console.error('error no entries found');
}
res.render('vendor', {
vendor: vendorName,
shop:doc
});
});
});
Any advice on how to build the necessary queries would be great. I've been reading (and practicing) with mongo aggregation but I'm not really near achieving what I would like. I'm hoping to be able to use the structure to also build URL patterns domain.com/:category, domain.com/:category/2017 etc
via Stuart Brown
No comments:
Post a Comment