1

I am trying to create a dynamic meteor collection using a variable so a new meteor collection will be created everytime an form is submitted and an event is executed. See code below for what I am looking for though is does not work. (Keep in mind I am still in the early production stages so I have not set up specific server or client side for debugging purposes. Also, disregard any grammatical or structure errors as i just typed this. just how to make it work)

Intended result:

Suppose user 1 meteor id is x533hf4j3i

Suppose user 2 meteor id is jf83jfu39d

OUTCOME: x533hf4j3ijf83jfu39d = new Mongo.Collection('x533hf4j3ijf83jfu39dmessages')


this sample code that DOES NOT WORK

Template.createChat.events({
  'submit form': function(event){
    event.preventDefault();
    var messageRecipientVar = event.target.messageRecipient.value;
    var currentUserId = Meteor.userId();
    var recipientUserId = Meteor.users.findOne(messageRecipientVar)._id;
    var chatCollectionNameVar = {$concat: [currentUserId, recipientUserId]}
    var chatCollectionName = {$concat: [currentUserId, recipientUserId, "messages"]}

    chatCollectionNameVar = new Mongo.Collection('chatCollectionName');
  }

});
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317

1 Answers1

2

Don't do this. Asking how to create dynamic collections comes up periodically with new meteor developers, but it's never the right approach. @david-wheldon has a great description of why not to do this at the bottom of this page.

Just use one collection Messages, containing documents something like this:

{ _id: xxxxxx,
  sender: 'x533hf4j3i',
  recipient:  'jf83jfu39d',
  message:    'Hi there!',
  ...
  timestamp, etc
  ...
}

Then it depends on your app if a user can view messages they did not send/receive, and if you need filtering on this you would do it server side in a publish function.

Either way, on the Client if you just want the messages between two users you would query like this:

chatMessages = Messages.find(
    {$or: [{ sender: 'x533hf4j3i', recipient:  'jf83jfu39d'}, 
           { sender: 'jf83jfu39d', recipient:  'x533hf4j3i'}
     ]}).fetch()
JeremyK
  • 3,240
  • 1
  • 11
  • 24
  • thank you for the tip. I figured that was the standard way to do it. but if i had one collection for all messages, i feel 1. it would take a long time to retrieve the current chats messages through a million messages (assuming i had a few thousand users) and 2. if someone hacked one collection they could have access to EVERYthing instead of just one chat. I am pretty new to this so what are your comments on that? thank you – Marshall Hawley Apr 15 '16 at 00:49
  • 2
    don't fall into the trap of optimizing prematurely! But when you need to, you can add indexes to your MongoDB collection to keep it fast. https://docs.mongodb.org/manual/indexes/. – JeremyK Apr 15 '16 at 02:47
  • Security? Control what is sent to the client server side with subscriptions. Hacking is not going to be 'per collection', and it's a much bigger question. Storing your data this way will not going to impact on the security of your app. – JeremyK Apr 15 '16 at 02:50
  • that you for the comments. and that index scanner looks great. have never see that before. thank you so much – Marshall Hawley Apr 15 '16 at 22:53