Search code examples
javascriptmongodbmeteormongodb-indexes

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


Solution

  • 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"
      }
    ]