node.js - is it possible to package a 'real' mongodb library for use on the *server* side only in meteor 0.6 -


i understand 'mini-mongo' packaged meteor till 0.6 had few limitations around aggregation, hoping story bit easier release of 0.6.

what i'm trying work around lack of 'aggregation' in mini-mongo.

i have mongodb records this

db.account_records.findone() {     "type" : "initial_balance",     "amt" : 10,     "account_id" : "95cpb9be8nx3tgspi",     "_id" : "l9d7agt4gw2ht4nta" } 

and want subscribe (at client) query following..

db.events.aggregate( [ { $group: {_id:"$account_id", balance: { $sum: "$amt"}}}]); 

.. gives (when run using "real" mongodb terminal "mongo meteor" style)..

{     "result" : [         {             "_id" : "dn5eouw8k4rrp9sgw",             "balance" : 169.99922000000308         },         {             "_id" : "bawopno2qgs8gumwy",             "balance" : 1         },         {             "_id" : "95cpb9be8nx3tgspi",             "balance" : 5         }     ],     "ok" : 1 } 

it seems unlikely me ever possible (or desirable) perform complex aggregate clauses client side can see reason have mini-mongo. hoping ability install standard npm modules in new version of meteor might bit easier create query can publish 'real' mongo , subscribe in mini-mongo?

--

ps found double underscores making me nervous i'd attempting complicated... https://github.com/meteor/meteor/pull/644

you can use aggregation push on server used. prior meteor 0.6.0 'engine' version npm module integration still unofficial hence underscores, can used officially below should work (from github pull modified work on 0.6.0 : https://github.com/meteor/meteor/pull/644)

server side js

var path = npm.require('path'); var mongodb = npm.require('mongodb'); var future = npm.require('fibers/future');  var animals = new meteor.collection("animals");  meteor.startup(function () {   animals.aggregate = function(pipeline) {     var self = this;      var future = new future;     self.find()._mongo.db.createcollection(self._name, function (err, collection) {       if (err) {         future.throw(err);         return;       }       collection.aggregate(pipeline, function(err, result) {         if (err) {           future.throw(err);           return;         }         future.ret([true, result]);       });     });     var result = future.wait();     if (!result[0])       throw result[1];      return result[1];   };  });  meteor.methods({   myaggregationmethod: function() {     return animals.aggregate([ {$project: {dog:1, age:1}}, {$sort:{age:1}} ]);   } }); 

and on client js, when want call aggregation:

    meteor.call('myaggregationmethod', function(err,result) {       if (!err) {           console.log(result)                 } else {         console.log(err);       }     }); 

adding mongodb package

1) make directory in project called packages store packages & in directory called mongodb mongodb npm package

2) add file in mongodb called package.js containing

package.describe({   summary: "mongodb driver" });  npm.depends({'mongodb':"1.2.14"});  package.on_use(function (api) {   api.add_files('lib.js', 'server'); }); 

add file in same mongodb directory called lib.js containing:

if(typeof(npm) != "undefined") {     mongodb = npm.require("mongodb"); } else {     console.log("please upgrade meteor 0.6.0")     mongodb = __meteor_bootstrap__.require("mongodb"); } 

finally remove line

var mongodb = npm.require("mongodb") server.js globally scoped in lib.js


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -