我在 appcloud 上部署了一个 nodejs 应用程序,使用 mongodb 作为服务,我想使用 mapReduce 进行一些查询,但我收到了这个错误:
2016-10-21 15:45:52 [APP/0] ERR ERR!{ [MongoError: 没有 js 引擎无法运行 map reduce]
swisscom appcloud 是否支持它?
这是我的控制器(摘录):
'use strict';
const mongo = require('../mongoclient');
const paramsParser = require('../paramsParser');
const log = require('npmlog');
const faker = require('faker');
const _ = require('lodash');
const datapoints = function (router) {
const map = function () {
var payload = this.payload;
if (payload) {
payload = payload.toLowerCase().split(" ");
for (var i = payload.length - 1; i >= 0; i--) {
payload[i] = payload[i].replace(/[^\w\s]|_/g, "").replace(/\s+/g, " ");
if (payload[i] && payload[i].length > 7) {
emit(payload[i], 1); // store a 1 for each word
}
}
}
}
const reduce = function(key, values) {
var count = 0;
values.forEach(function (v) {
count += v;
});
return count;
}
router.get('/counts', function (req, res) {
const filters = paramsParser.parse(req.query);
mongo.mapReduce(map, reduce, filters)
.then(function (data) {
const topics = data
.sort((a, b) => b.value - a.value)
.slice(0, 10)
.map(function(topic) {
return { id: faker.random.uuid(), title: topic._id, score: topic.value }
});
res.json(topics);
})
.catch(function(err) {
log.error(err);
res.sendStatus(500);
});
});
};
module.exports = datapoints;
function mapReduce(map, reduce, filters) {
filters = filters ? filters : defaults;
return new Promise(function(resolve, reject) {
client.connect(uri(), function(err, db) {
db.collection(collection)
.mapReduce(map, reduce, { out: { inline: 1 }, query: filters.find, limit: filters.pageSize }, function(err, docs) {
if (err) {
reject(err);
}
resolve(docs);
});
});
});
}