Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

mongodb - Meteor, define collections dynamically

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...