4

I tried to use:

MongoInternals.defaultRemoteCollectionDriver().mongo.db.listCollections()

in order to get all collection names in meteor database, but it returns a very long JSON, in which i couldn't find the pure collection names. (see the attached image)

enter image description here

How can I get meteor collection names in a following format:

["test1", "test2", "users"...]
Cœur
  • 37,241
  • 25
  • 195
  • 267
Dariusz Sikorski
  • 4,309
  • 5
  • 27
  • 44
  • 1
    `listCollections` returns a `MongoCollection` which you would iterate over, calling `.getNamespace()` to get the names. Please note the returned value you are seeing is **not** JSON but rather the console's interpretation of the object, hence items such as `[Function]` – Paul S. Jan 01 '16 at 13:19
  • @PaulS. thanks for clearing this out I will implement code based on this information. – Dariusz Sikorski Jan 01 '16 at 13:40

1 Answers1

8

Ok, here is the working code, thanks @PaulS.

db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
collections = db.listCollections();

collections.each(function(n, collection){
  if(collection){
    console.log( collection.name );
  }
});
Dariusz Sikorski
  • 4,309
  • 5
  • 27
  • 44