2015-10-29 2 views
1

Я пытаюсь подключиться к локальному серверу mongodb в приложении meteor, чтобы получить список всех коллекций в рамках серверной функции.Метеор: выполните meteor mongo в приложении и получите все имена

Следующая command переменная будет выполнять и возвращает

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

Теперь я хотел бы выполнить переменную commandWanted, с целью получить список всех коллекций Монго 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

Используйте 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(); 
}; 
Смежные вопросы