1

We are working on an app and need to be able to create new Mongo collections on the fly. Currently we have code such as this:

@Global = new Meteor.Collection('global')

We have a document in this mongo collections that looks like this:

{ "title" : "room_list", "room_list" : ['chat1', 'chat2'], ... }

Now I want to set up some type of loop or construct that would basically create the following

@chat1 = new Meteor.Collection('chat1')
@chat2 = new Meteor.Collection('chat2')

We are seeming to need this type of functionality to be able to create new collections of data on the fly.

We are looking into some type of dynamic variable declaration or is there a better way to dynamically create new meteor collections?

Scalahansolo
  • 2,615
  • 6
  • 26
  • 43

1 Answers1

1

In most instances, you probably don't want to create multiple collections, but instead use one collection and send views of it to clients depending on their subscription.

You may want to check out the https://github.com/mizzao/meteor-partitioner package I've built which is designed especially for this purpose, and includes an example for how to do this for multiple chat rooms. You can also see https://github.com/mizzao/CrowdMapper for an implemented example.

Andrew Mao
  • 35,740
  • 23
  • 143
  • 224
  • This looks promising, but I am concerned about one thing. In our app, we want our users to be able to be in more than one chat room at any given time and it seems like with partitioner, that is not possible? Partitioner.setUserGroup(userId, groupId) in your API docs says it will throw errors if the user tries to be in more than one at a time? – Scalahansolo Jun 24 '14 at 19:40
  • You wouldn't want to use partitioner for multiple chat rooms at once; you'd just create your own indexing scheme. In any case, the chat messages should be in the same collection and there should be an index for the room of each message, rather than a separate collection for each room. – Andrew Mao Jun 24 '14 at 19:43
  • We have some other things beyond just using the collection for just messages. So back to the original question, is it possible to set up dynamic rooms such as I have written above? – Scalahansolo Jun 24 '14 at 19:47
  • 1
    You can, but you'll probably run into limits on the [maximum number of collections you can have in Mongo](http://docs.mongodb.org/manual/reference/limits/). I don't see any reason why you would want to do it this way. If you would just specify your problem a bit more concisely (perhaps in a different question) I'm sure someone could come up with a satisfactory approach. – Andrew Mao Jun 24 '14 at 19:56