meteorBuzz
meteorBuzz

Reputation: 3200

Meteor: List all collections on the server

I am unable to list all the collections created within the Meteor app on the server.

The collections sit... /root/packages/lib/collectionName.js

Operations to collectionName succeeds. However, as I have many collections, I require a function to retrieve all the names of the collections created.

In a server function after the collections have been loaded, a method executes the following function;

MyCollection = Mongo.Collection.getAll();
console.log(MyCollection);


        [ { name: 'users',
       instance: 
          { _makeNewID: [Function],
            _transform: null,
            _connection: [Object],
            _collection: [Object],
            _name: 'users',
            _driver: [Object],
            _restricted: true,
            _insecure: undefined,
            _validators: [Object],
            _prefix: '/users/' },
        options: undefined },
       { name: 'MeteorToys/Impersonate',
         instance: 
         { _makeNewID: [Function],
         ....

The user collection created by Meteor is there. But all the collections created, 3 of which are with Collection2 package do not show.

What am I missing?

What are the implications if I require the child_process library and execute a series of commands.. "meteor mongo"... "db.getCollectionNames()"?

Upvotes: 4

Views: 4242

Answers (1)

chridam
chridam

Reputation: 103475

To list all collections on the server, use the RemoteCollectionDriver to access preexisting MongoDB Collections. To implement this server side, you could follow this asynchronous pattern:

var shell = function () {
    var Future = Npm.require('fibers/future'),
        future = new Future(),
        db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;

    db.collectionNames( 
        function(error, results) {
            if (error) throw new Meteor.Error(500, "failed");
            future.return(results);
        }
    );
    return future.wait();
};

Upvotes: 6

Related Questions