【问题标题】:Meteor: execute meteor mongo within app and get collection all namesMeteor:在应用程序中执行meteor mongo并收集所有名称
【发布时间】:2015-10-29 18:19:14
【问题描述】:

我正在尝试连接到流星应用程序中的本地 mongodb 服务器,以便我可以获取服务器端函数中所有集合的列表。

下面的command变量确实执行并返回

/Users/myProject/.meteor/local/build/programs/server

现在我想执行 commandWanted 变量,旨在检索所有 mongo db 集合的列表。

server.js

var Future = Meteor.npmRequire("fibers/future");
var exec = Npm.require('child_process').exec;


function shell() {
    var future = new Future();
    var command = "pwd";
    var commandWanted = "meteor mongo" + "db.getCollectionNames()";
    exec(commandWanted, function(error,stdout,stderr){
            if (error) {
                    console.log(error);
                    throw new Meteor.Error(500, "failed");
            }
            console.log(stdout.toString());
            future.return(stdout.toString());
    });
    return future.wait();
}


shell();

【问题讨论】:

    标签: bash mongodb shell meteor


    【解决方案1】:

    使用 RemoteCollectionDriver 访问预先存在的 MongoDB 集合

    var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
    db.collectionNames(function(err, collections) {
        if (err) throw err;
        console.log(collections);
    });
    
    // for commands not natively supported by the driver - http://docs.mongodb.org/manual/reference/command/
    db.command({profile: 1}, function(error, result) {
        if (error) throw error;
        if (result.errmsg)
            console.error('Error calling native command:', result.errmsg);
        else
            console.log(result);
    });
    

    要实现这个服务器端,你可以遵循这个异步模式:

    var shell = function () {
        var Future = Npm.require('fibers/future'),
            future = new Future(),
            db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
    
        db.collectionNames( 
            function(error, results) {
                if (err) throw new Meteor.Error(500, "failed");
                future.return(results);
            }
        );
        return future.wait();
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-06
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      相关资源
      最近更新 更多