在 Meteor 中获取集合索引的列表

Get a list of collection's indexes in Meteor

本文关键字:索引 列表 集合 获取 Meteor      更新时间:2023-09-26
如何使用

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

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

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

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