Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a list of collection's indexes in Meteor

How does one get the list of indexes on a collection with Meteor?
Something similar to (or perhaps based on, proxying) Mongo's db.collection.getIndexes
There's not much of an indexing API in Meteor yet (there will be one eventually); but I hope someone has already solved this problem
Cheers

like image 323
Bogdan D Avatar asked Jan 20 '15 10:01

Bogdan D


1 Answers

According to this issue, you can add a getIndexes to the Mongo Collection prototype like this (credit to @jagi):

if (Meteor.isServer) {
  var Future = Npm.require('fibers/future');
  Mongo.Collection.prototype.getIndexes = function() {
    var raw = this.rawCollection();
    var future = new Future();

    raw.indexes(function(err, indexes) {
      if (err) {
        future.throw(err);
      }

      future.return(indexes);
    });

    return future.wait();
  };

  Items = new Mongo.Collection();
  console.log(Items.getIndexes());
}

You can also open the Mongo DB shell and access the Mongo db collections directly.

meteor mongo
meteor:PRIMARY> db.tags.getIndexes()
[
  {
    "v" : 1,
    "key" : {
      "_id" : 1
    },
    "name" : "_id_",
    "ns" : "meteor.tags"
  }
]
like image 52
Nate Avatar answered Oct 19 '22 22:10

Nate