patternjavascriptMinor
Optimizing async joins for mongodb (mongoose) using async.js
Viewed 0 times
mongooseasyncmongodboptimizingforusingjoins
Problem
I'm building a media viewer web app for a kiosk in node that uses data in mongo. To render the viewer, it gathers together all the Asset objects (representing video files), each of which belong to a Category, each of which belong to a Medium.
This is my first time using mongo and node together, so I decided to use async.js to organize the code, but I don't know if I'm doing things efficiently or not. Is there a better way to organize the data or perform the queries? Does this look remotely sane?
Additionally, I commented out some code I thought looked correct but would mysteriously overwrite the object simply by adding another key to it. What am I doing wrong?
models.js : mongoose models
viewer.js : the handler that retrieves all the data and renders the html
```
exports.index = function(req, res){
res.render('index');
};
exports.viewer = function(req, res) {
var models = require('./models');
var async = require("async");
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connec
This is my first time using mongo and node together, so I decided to use async.js to organize the code, but I don't know if I'm doing things efficiently or not. Is there a better way to organize the data or perform the queries? Does this look remotely sane?
Additionally, I commented out some code I thought looked correct but would mysteriously overwrite the object simply by adding another key to it. What am I doing wrong?
models.js : mongoose models
var mongoose = require('mongoose');
/* Mediums... Film, Video, TV */
var mediumSchema = mongoose.Schema({
name: String,
id: String,
description: String
});
var Medium = mongoose.model('mediums', mediumSchema);
/* Categories ... */
var categorySchema = mongoose.Schema({
medium: String, // refers to medium.id, not medium._id
name: String,
shortName: String,
years: String,
description: String
});
var Category = mongoose.model('categories', categorySchema);
/* Assets */
var assetSchema = mongoose.Schema({
title: String
, year: Number
, duration: Number
, medium_info: String
, copyright: String
, extended_info: String
, filename: String
, category_id: mongoose.Schema.Types.ObjectId
});
var Asset = mongoose.model('assets', assetSchema);
exports.Medium = Medium;
exports.Category = Category;
exports.Asset = Asset;viewer.js : the handler that retrieves all the data and renders the html
```
exports.index = function(req, res){
res.render('index');
};
exports.viewer = function(req, res) {
var models = require('./models');
var async = require("async");
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connec
Solution
A short review;
-
My assumption is that
I would generate a javascript file with the meta data and categories in 1 big Object Notation assignment, and re-generate it whenever mediums/categories change, this would greatly simplify your code, and make it way faster.
-
With Object Notation, this
would be
note that you override the results of
- Consider using JsHint
- Declare
datawithvar, otherwise you are polluting the global namespace
- Consider
errmore often, the assumption that there will be no error will bite you
- Do not simply log to
console.log, it is one of the most common bottlenecks
- Remove your commented out code, it keeps things clean
-
My assumption is that
- The meta data of
["FILM","VIDEO","TV"]does not change often
- The categories do no change often
I would generate a javascript file with the meta data and categories in 1 big Object Notation assignment, and re-generate it whenever mediums/categories change, this would greatly simplify your code, and make it way faster.
-
With Object Notation, this
data[mediumName] = {};
data[mediumName]["categories"] = categoryResults;
data[mediumName]["description"] = mediumResults.description;
data[mediumName]["name"] = mediumResults.name;would be
data[mediumName] = {
categories : [];
description : mediumResults.description;
name : mediumResults.name;
};note that you override the results of
data[mediumName]["categories"] = categoryResults; with data[mediumName].categories = [];Code Snippets
data[mediumName] = {};
data[mediumName]["categories"] = categoryResults;
data[mediumName]["description"] = mediumResults.description;
data[mediumName]["name"] = mediumResults.name;data[mediumName] = {
categories : [];
description : mediumResults.description;
name : mediumResults.name;
};Context
StackExchange Code Review Q#44776, answer score: 5
Revisions (0)
No revisions yet.