我创建了一个包含Node JS,Express JS,Mongoose和D3 JS的网页。缓慢加载节点JS中的大型MongoDB数据库
在网页中,它包含3个下拉菜单:Department,Employee,Week。 网页的用法如下:
- 当选择“部门”,“雇员”菜单将被过滤,只显示那些从选定的“部门”。选择'员工'后,同样会进入“周”。
- 选择3个菜单并单击'PLOT'按钮后,会绘制折线图(使用d3.js)以显示该月的员工工作时间。
的MongoDB的Json
{ dep: '1',
emp: 'Mr A',
week: 1,
hrs: [{
{1,8},
{2,10},
...
}]
}
这里是我的代码片段:
routes.js
// Connect the required database and collection
var dataAll = require('./models/dataModel');
module.exports = function(app) {
app.get('/api/data', function(req, res) {
dataAll.find({}, {}, function(err, dataRes) {
res.json(dataRes);
});
}
app.get('*', function(req,res) {
res.sendfile('./index.html');
}
}
个的index.html
... // More codes
<div id="menuSelect1"></div>
<div id="menuSelect2"></div>
<div id="menuSelect3"></div>
...
<script src="./display.js" type='text/javascript'></script>
... // More codes
display.js
//Menu (Department,Employee,Week) Information is gathered here
queue()
.defer(d3.json, "/api/data")
.await(createPlot);
function createPlot(error, plotData) {
var myData = plotData;
var depData = d3.nest()
.key(function(d) {return d.dep;})
.rollup(function(v) {return v.length;})
.entries(myData);
selectField1 = d3.select('#menuSelect1')
.append("select")
.on("change", menu1change)
.selectAll(depData)
.enter()
.append("option")
.attr("value", function(d) {return d.key;})
.text(function(d) {return d.key;});
function menu1Change() {
//Filter Next Menu with the option chosen in this menu
... // More codes
var selectedVal = this.options[this.selectedIndex].value;
var empData = dataSet.filter(function(d) { return d.emp = selectString; });
... // More codes
}
... // More codes
}
问题: 功能上,它工作正常。问题是数据库变得越来越大,页面的加载变得非常慢(分钟加载)。我相信这应该归功于所有数据检索的路由(.find({},{})),但我认为我需要它,因为我在'display.js'中使用它来过滤我的菜单选项。
有没有更好的方法来解决性能问题?
对不起,添加了JSON数据实例,是的,我已经在浏览器中使用了调试器,瓶颈确实还在整个数据库。 – ISee
如果我没有获取整个数据库,是否有其他方法可以将所需的Dep,Emp,Mth数据插入到下拉菜单中? – ISee