我正在编写一个远程方法,通过运行聚合管道查询可以大大增强该方法.
要做到这一点,我需要获得实际的mongodb连接并直接使用它.
我怎么能运行一些东西
module.exports = function(ZipCodes) {
ZipCodes.pipeline = function (cb) {
//Get the MongoDB Connection
var mongodbConnection = ***whatever magic***
var result = mongodbConnection.db.zipcodes.aggregate( { $group :
{ _id : "$state",
totalPop : { $sum : "$pop" } } },
{ $match : {totalPop : { $gte : 10*1000*1000 } } } );
cb(result);
};
ZipCodes.remoteMethod('pipeline', {
returns: {arg: 'zips', type: 'array', root: false},
http: {path:'/pipeline', verb: 'get'}
});
};
Run Code Online (Sandbox Code Playgroud)
我在我的datasources.json中定义了mongo
{
"db": {
"name": "db",
"connector": "memory"
}, …Run Code Online (Sandbox Code Playgroud)