获取Meteor中的集合索引列表

Bog*_*n D 6 javascript mongodb meteor mongodb-indexes

如何使用Meteor获取集合上的索引列表?
类似于(或者可能基于代理)Mongo的 东西在Meteor中还没有太多的索引API(最终会有一个); 但我希望有人已经解决了这个问题 干杯db.collection.getIndexes

Nat*_*ate 4

根据这个问题,你可以getIndexes像这样添加一个到 Mongo Collection 原型(归功于@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());
}
Run Code Online (Sandbox Code Playgroud)

您还可以打开 Mongo DB shell 并直接访问 Mongo db 集合。

meteor mongo
meteor:PRIMARY> db.tags.getIndexes()
[
  {
    "v" : 1,
    "key" : {
      "_id" : 1
    },
    "name" : "_id_",
    "ns" : "meteor.tags"
  }
]
Run Code Online (Sandbox Code Playgroud)