【问题标题】:Get a list of collection's indexes in Meteor在 Meteor 中获取集合的索引列表
【发布时间】:2015-03-18 12:41:34
【问题描述】:

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

【问题讨论】:

  • 您始终可以访问底层的本机驱动程序对象,甚至基本上可以从中访问Db 对象。可以做到。寻找那个,但如果你没有先找到它,我会让其他人成为英雄。
  • 您可以使用_.map函数创建索引,当然只有当您需要它在网站上打印索引时。
  • @NeilLunn,听起来不错,我最终会研究一下,也许会受到_ensureIndex 的实施的启发。 @Sindis,我不确定我们是否在同一页面上(而且我不是在谈论网站页面:)我的问题是关于 Mongo 集合中的索引字段。目标是在数据库中调用find,它只查看集合的索引键。对不起,如果问题的文字令人困惑..

标签: javascript mongodb meteor mongodb-indexes


【解决方案1】:

根据this issue,您可以像这样将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());
}

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

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

【讨论】:

    猜你喜欢
    • 2019-08-20
    • 2012-10-31
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2012-11-07
    相关资源
    最近更新 更多