【问题标题】:Meteor: List all collections on the serverMeteor:列出服务器上的所有集合
【发布时间】:2015-11-20 12:28:49
【问题描述】:

我无法列出服务器上 Meteor 应用程序中创建的所有集合。

集合坐.../root/packages/lib/collectionName.js

collectionName 操作成功。但是,由于我有很多集合,我需要一个函数来检索创建的集合的所有名称。

在一个服务器函数中集合被加载之后,一个方法执行下面的函数;

MyCollection = Mongo.Collection.getAll();
console.log(MyCollection);


        [ { name: 'users',
       instance: 
          { _makeNewID: [Function],
            _transform: null,
            _connection: [Object],
            _collection: [Object],
            _name: 'users',
            _driver: [Object],
            _restricted: true,
            _insecure: undefined,
            _validators: [Object],
            _prefix: '/users/' },
        options: undefined },
       { name: 'MeteorToys/Impersonate',
         instance: 
         { _makeNewID: [Function],
         ....

Meteor 创建的用户集合在那里。但是所有创建的集合,其中3个带有Collection2包的集合都没有显示。

我错过了什么?

如果我需要 child_process 库并执行一系列命令..“meteor mongo”...“db.getCollectionNames()”会有什么影响?

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    要列出服务器上的所有集合,请使用 RemoteCollectionDriver 访问预先存在的 MongoDB 集合。要实现此服务器端,您可以遵循以下异步模式:

    var shell = function () {
        var Future = Npm.require('fibers/future'),
            future = new Future(),
            db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
    
        db.collectionNames( 
            function(error, results) {
                if (error) throw new Meteor.Error(500, "failed");
                future.return(results);
            }
        );
        return future.wait();
    };
    

    【讨论】:

    • 我使用 connect 连接到本地数据库 url。这个答案很完美。谢谢你!
    • 啊,这就是您访问原始数据库的方式?它的行为方式是否与meteor mongo 相同?
    • @corvid 是的,因为 Meteor 构建在 MongoDB 之上,而 Meteor 目前严重依赖 MongoDB。您还可以通过Mongo.Collection 上的rawCollectionrawDatabase methods 获得对npm MongoDB 驱动程序中的集合和数据库对象的原始访问权限。
    • @DariuszSikorski 感谢您注意到错字:)
    猜你喜欢
    • 2016-05-26
    • 1970-01-01
    • 2013-06-29
    • 2018-02-03
    • 2014-06-03
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多