1

Normally, MongoDB Collections are defined like this:

DuckbilledPlatypi = new Mongo.Collection("duckbilledplatypi"); 

I want to, though, dynamically generate Collections based on user input. For example, I might want it to be:

RupertPupkin20151212_20151218 = new Mongo.Collection("rupertPupkin20151212_20151218"); 

It would be easy enough to build up the Collection name:

var dynCollName = username + begindate +'_'+ enddate;

...and then pass "dynCollName") to Mongo.Collection:

 = new Mongo.Collection(dynCollName); 

...but what about the Collection instance name - how can that be dynamically generated? I would need something like:

"RupertPupkin20151212_20151218".ToRawName() = new Mongo.Collection(dynCollName);

-or:

"RupertPupkin20151212_20151218".Unstringify() = new Mongo.Collection(dynCollName);

...but AFAIK, there's no such thing...

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    This has been asked and answered several times. No - don't do this. I recently added a section to [common mistakes](http://dweldon.silvrback.com/common-mistakes) to make this point clear. – David Weldon Sep 15 '15 at 22:07
  • Thanks for that; I'll go over that page of yours with a fine-toothed comb. – B. Clay Shannon-B. Crow Raven Sep 15 '15 at 22:14
  • 1
    Really bad idea. Why do you want to do this? Whats the underlying problem you're trying to solve? – Michel Floyd Sep 15 '15 at 22:29
  • possible duplicate of [Meteor, define collections dynamically](http://stackoverflow.com/questions/24394680/meteor-define-collections-dynamically) – Christian Fritz Sep 15 '15 at 22:37
  • I hadn't definitely decided to do that, just wondering if it's possible while I ponder the possibility. In any case, I will have to have master/detail Collections, but my "fear" is that, if I keep all the data in one static Collection, the Collection will get huMONGOus (maybe that's not a problem) and performance would suffer. – B. Clay Shannon-B. Crow Raven Sep 15 '15 at 22:40
  • 1
    Look into adding Indexes to your Collections if performance is an issue. http://docs.mongodb.org/manual/indexes/ – JeremyK Sep 15 '15 at 23:15

1 Answers1

3

On a single client instance, yes, and you could dynamically reference it. However in the general case (using it to sync data between the server and all connected clients), no.

I address this point in the Dynamically created collections section of common mistakes in a little detail, but the fundamental problem is that it would be highly complex to get all connected clients to agree on a dynamically generated set of collections.

It's much more likely that a finite set of collections where some have a flexible schema, is actually what you want. As Andrew Mao points out in the answer to this related question, partitioner is another tool available to help address some cases which give rise to this question.

Community
  • 1
  • 1
David Weldon
  • 63,632
  • 11
  • 148
  • 146