我正在尝试连接到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();
答案 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();
};