流星:在app中执行meteor mongo并获取所有名称的集合

时间:2015-10-29 09:44:52

标签: bash mongodb shell meteor

我正在尝试连接到meteor应用程序中的本地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();

1 个答案:

答案 0 :(得分:0)

使用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();
};