【问题标题】:Creating new Meteor collections on the fly即时创建新的 Meteor 集合
【发布时间】:2013-02-19 07:33:30
【问题描述】:

是否可以即时创建新的 Meteor 收藏?我想创建 foo_barbar_bar 取决于我认为应该是全局变量的路径名(这样我就可以在整个应用程序中访问它)。
比如:

var prefix = window.location.pathname.replace(/^\/([^\/]*).*$/, '$1');
var Bar = new Meteor.Collection(prefix+'_bar');

这里的问题是我应该从 URL 中获取我的 prefix 变量,所以如果我在 if (Meteor.isClient) 之外声明它,我会收到一个错误:ReferenceError: window is not defined。有可能做这样的事情吗?

编辑:使用 Akshats 的第一次迭代回答我的项目 js:http://pastie.org/6411287

【问题讨论】:

  • 即使有可能,但这并不意味着它是一个好主意:MongoDB 对集合的数量有一些限制,它并不意味着按集合扩展,所以如果你的动态集合增长大,你可能会撞墙。

标签: mongodb meteor mongo-collection


【解决方案1】:

事情变得容易多了:

var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
db.createCollection("COLLECTION_NAME", (err, res) => {
    console.log(res);
});

在你的服务器方法中运行它。

【讨论】:

    【解决方案2】:

    我不完全确定这会奏效:

    您需要它分为两部分,第一部分用于加载您之前设置的集合(在客户端和服务器上)

    var collections = {};
    var mysettings = new Meteor.Collection('settings') //use your settings
    
    //Startup
    Collectionlist = mysettings.find({type:'collection'});
    
    Collectionlist.forEach(function(doc) {
        collections[doc.name] = new Meteor.Collection(doc.name);
    })'
    

    你需要一点点来在服务器上添加集合:

    Meteor.methods({
        'create_server_col' : function(collectionname) {
           mysettings.insert({type:'collection', name: collectionname});
           newcollections[collectionname] = new Collection(collectionname);
           return true;
        }
    });
    

    你需要在客户端创建它们:

    //Create the collection:
    
    Meteor.call('create_server_col', 'My New Collection Name', function(err,result) {
        if(result) {
            alert("Collection made");
        }
        else
        {
            console.log(err);
        }
    }
    

    再一次,这一切都未经测试,所以我只是试一试,希望它有效。

    编辑

    也许下面应该可以工作,我已经添加了一些检查以查看集合是否首先存在。请您在使用meteor reset 对上述代码中的错误进行排序之前运行它:

    var collections = {};
    var mysettings = new Meteor.Collection('settings')
    
    if (Meteor.isClient) {
      Meteor.startup(function() {
        Collectionlist = mysettings.find({type:'collection'});
    
        Collectionlist.forEach(function(doc) {
            eval("var "+doc.name+" = new Meteor.Collection("+doc.name+"));
        });
      });
      Template.hello.greeting = function () {
        return "Welcome to testColl.";
      };
    
      var collectionname=prompt("Enter a collection name to create:","collection name")
    
      create_collection(collectionname);
    
      function create_collection(name) {
        Meteor.call('create_server_col', 'tempcoll', function(err,result) {
            if(!err) {
                if(result) {
                    //make sure name is safe
                    eval("var "+name+" = new Meteor.Collection('"+name+"'));
                    alert("Collection made");
                    console.log(result);
                    console.log(collections);
                } else {
                    alert("This collection already exists");
                }
            }
            else
            {
                alert("Error see console");
                console.log(err);
            }
        });
      }
    }
    
    if (Meteor.isServer) {
      Meteor.startup(function () {
        // code to run on server at startup
        Collectionlist = mysettings.find({type:'collection'});
    
        Collectionlist.forEach(function(doc) {
            collections[doc.name] = new Meteor.Collection(doc.name);
        });
      });
    
      Meteor.methods({
        'create_server_col' : function(collectionname) {
           if(!mysettings.findOne({type:'collection', name: collectionname})) {
            mysettings.insert({type:'collection', name: collectionname});
    
            collections[collectionname] = new Meteor.Collection(collectionname);
    
            return true;
           }
           else
           {
            return false; //Collection already exists
           }
        }
      });
    }
    

    还要确保你的名字是 javascript 转义的。

    【讨论】:

    • 感谢您提供帮助 :) 我一直在玩弄您的代码,但注意到了一些事情。首先,我必须更改一些拼写错误,我的新代码是here。运行此程序后,我可以看到警报,因此显然创建了集合,但是当我尝试查询我的新集合时,我得到ReferenceError: tempcoll is not defined。同样在警报出现后,如果我尝试在控制台中查看我的 collections 对象,我发现它是空的。
    • 最后,如果我刷新代码中具有相同集合名称的页面,我会得到Exception while invoking method 'create_server_col' Error: A method named '/tempcollx/insert' is already defined。所以我想我应该检查是否已经存在同名的集合。我确实相信您对您的代码非常了解,也许有些东西完全是微不足道的,但是我对 mongodb 没有太多经验,所以我不知道如何进一步调试...感谢您再次提供帮助:)
    • 如果你没问题,我会用你粘贴的代码来回答,我可以编辑你的问题以包含你的要点链接吗?
    • 好了,抱歉让您久等了,这应该离您想要的更近了
    猜你喜欢
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多