【发布时间】:2017-01-03 03:37:10
【问题描述】:
这是我制作应用程序所需的所有代码。因此,我可以从 csv 文件导入数据/记录,也可以从 localhost:3000 的应用程序手动插入,然后将其插入数据库。从那里我将能够从这里查询。错误位于底部的 server.js 中。 bulk 和 avg 似乎都没有执行。
server.js,其中包括批量(1000 条数据及更多)和平均查询(空气温度)
var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('meibanlist', ['meibanlist']);
var bodyParser = require('body-parser');
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.get('/meibanlist', function (req, res) {
console.log('I received a GET request');
db.meibanlist.find(function (err, docs) {
console.log(docs);
res.json(docs);
});
});
app.post('/meibanlist', function (req, res) {
console.log(req.body);
db.meibanlist.insert(req.body, function(err, doc) {
res.json(doc);
});
});
app.delete('/meibanlist/:id', function (req, res) {
var id = req.params.id;
console.log(id);
db.meibanlist.remove({_id: mongojs.ObjectId(id)}, function (err, doc) {
res.json(doc);
});
});
app.get('/meibanlist/:id', function (req, res) {
var id = req.params.id;
console.log(id);
db.meibanlist.findOne({_id: mongojs.ObjectId(id)}, function (err, doc) {
res.json(doc);
});
});
app.put('/meibanlist/:id', function (req, res) {
var id = req.params.id;
console.log(req.body.machine_unit);
db.meibanlist.findAndModify({
query: {_id: mongojs.ObjectId(id)},
update: {$set: {machine_unit: req.body.machine_unit, air_temperature: req.body.air_temperature, water_temperature: req.body.water_temperature, heat_temperature: req.body.heat_temperature, room_temperature: req.body.room_temperature, date: req.body.date, time: req.body.time}},
new: true}, function (err, doc) {
res.json(doc);
}
);
});
var cursor = db.meibanlist.find({"air_temperature": { "$exists": true, "$type": 2 }}),
bulkUpdateOps = [];
cursor.forEach(function(doc){
var newTemp = parseInt(doc.air_temperature, 10);
bulkUpdateOps.push({
"updateOne": {
"filter": { "_id": doc._id },
"update": { "$set": { "air_temperature": newTemp } }
}
});
if (bulkUpdateOps.length === 500) {
db.meibanlist.bulkWrite(bulkUpdateOps);
bulkUpdateOps = [];
}
});
if (bulkUpdateOps.length > 0) { db.meibanlist.bulkWrite(bulkUpdateOps); }
db.meibanlist.aggregate([
{
"$group": {
"_id": null,
"averageAirTemperature": { "$avg": "$air_temperature" }
}
}
], function(err, results){
if (err || !results) console.log ("record not found");
else console.log(results[0]["averageAirTemperature"]);
});
app.listen(3000);
console.log("Server running on port 3000");
index.html
<!DOCTYPE>
<html ng-app="myApp">
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<title>Meiban App</title>
</head>
<body>
<div class="container" ng-controller="AppCtrl">
<h1>Meiban App</h1>
<table class="table">
<thead>
<tr>
<th>Machine unit</th>
<th>Air Temperature</th>
<th>Water Temperature</th>
<th>Heat Temperature</th>
<th>Room Temperature</th>
<th>Date</th>
<th>Time</th>
<th>Action</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="form-control" ng-model="contact.machine_unit"></td>
<td><input class="form-control" ng-model="contact.air_temperature"></td>
<td><input class="form-control" ng-model="contact.water_temperature"></td>
<td><input class="form-control" ng-model="contact.heat_temperature"></td>
<td><input class="form-control" ng-model="contact.room_temperature"></td>
<td><input class="form-control" ng-model="contact.date"></td>
<td><input class="form-control" ng-model="contact.time"></td>
<td><button class="btn btn-primary" ng-click="addCollection()">Add Collection</button></td>
<td><button class="btn btn-info" ng-click="update()">Update</button> <button class="btn btn-info" ng-click="deselect()">Clear</button></td>
</tr>
<tr ng-repeat="contact in collection">
<td>{{contact.machine_unit}}</td>
<td>{{contact.air_temperature}}</td>
<td>{{contact.water_temperature}}</td>
<td>{{contact.heat_temperature}}</td>
<td>{{contact.room_temperature}}</td>
<td>{{contact.date}}</td>
<td>{{contact.time}}</td>
<td><button class="btn btn-danger" ng-click="remove(contact._id)">Remove</button></td>
<td><button class="btn btn-warning" ng-click="edit(contact._id)">Edit</button></td>
</tr>
</tbody>
</table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.12/angular.min.js"></script>
<script src="controller/controller.js"></script>
</body>
</html>
controller.js
var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
console.log("Hello World from controller");
var refresh = function() {
$http.get('/meibanlist').success(function(response) {
console.log("I got the data I requested");
$scope.meibanlist = response;
$scope.contact = "";
});
};
refresh();
$scope.addCollection = function() {
console.log($scope.contact);
$http.post('/meibanlist', $scope.contact).success(function(response) {
console.log(response);
refresh();
});
};
$scope.remove = function(id) {
console.log(id);
$http.delete('/meibanlist/' + id).success(function(response) {
refresh();
});
};
$scope.edit = function(id) {
console.log(id);
$http.get('/meibanlist/' + id).success(function(response) {
$scope.contact = response;
});
};
$scope.update = function() {
console.log($scope.contact._id);
$http.put('/meibanlist/' + $scope.contact._id, $scope.contact).success(function(response) {
refresh();
})
};
$scope.deselect = function() {
$scope.contact = "";
}
}]);
【问题讨论】:
-
您为什么要在一个结果 (
Average) 中对两个单独的字段($length和$breadth)进行平均? -
这只是一个示例。我有一个完全不同的领域。我只需要放一些东西来区分。实际上是价格和数量
-
但关键是您正试图神奇地将这两个字段合二为一。你想要平均价格吗?数量的平均值?这两个值的乘积的平均值?或者完全是其他东西的平均值?
-
我需要找到平均价格。
-
那么我想你会想要像
{"Average": {$avg: ["$price"]}}这样的东西。
标签: javascript node.js mongodb mongodb-query aggregation-framework