Connect to Collection dynamically based on user input

I’m new to Meteor and Mongo so there’s a good chance that this is an easy solve, but I’ve been racking my brains for days and can’t figure out a way to do it.

Basically, I have a collection that contains a bunch of documents that render to a web page as a list. When I user chooses an item, that item corresponds to a collection whose name matches the ID of the item. So once selected, I want to retrieve the contents of the collection that is referenced by that document.

In effect I want to be able to dynamically update:

dynamicCollection = new Mongo.Collection(selectedItemId);

Where selectedItemId is a dynamically updated variable based on a user selection.

I’ve tried using global and window references but just keep getting errors when calling find().

Hmm, this sounds like a very unusual approach, and I’m not sure it’s the best. What are you trying to do? Are you perhaps trying to create a category system or something? If so, it’s better to have all items in the same collection, and give each a “category” field, which you can use to filter them.

2 Likes

Hi. Thanks for your reply.

I’m effectively trying to use it as an indexing system. So a master collection has an entry for each collection with some basic info. That master collection i render to a web page, the user clicks the item that they want and a bunch of time series data is retrieved from the collection that they selected to render charts, etc.

Kind of thought that keeping the thousands of time series points organised by collection made sense as in a sql world, I’d do that by separate tables and it would be pretty straight forward to do. Maybe I’m just not in the NoSQL mindset.

Any guidance gratefully accepted.

https://atmospherejs.com/dburles/mongo-collection-instances

And use .get(selectedItemId)

1 Like

I’d do the same thing with SQL - organising one table by category. As long as your category is indexed there’ll be no performance penalty, whether it’s SQL or MongoDB - and the design and implementation will be so much easier.

2 Likes

Thanks, but if I haven’t explicitly called new Meteor.Collection() new Meteor.Collection() for each collection that I might want to query using this package, I’m guessing it won’t work?

The problem I think I’ve got is that I’m trying to dynamically call new Meteor.Collection(selectedItemId) but that doesn’t seem to work as it ends up running on the client instead of the server.

Like I say, new to this, so almost certainly making a dumb rookie mistake.

I’m sure there’s a way to do it, but personally I think it’s a strange decision. Couldn’t you just use one general collection and then add a field {selectedItemId: yourid} so you can query it?

Quick example:
You have a collection listItems. When a user clicks an item from that list it queries another collection listDetails with the _id of the listItem, like:

listDetails.find({itemId: yourlistItemId})

I don’t see the need for a strange solution with a bunch of collections in your case.

2 Likes

I’m certainly leaning that way. Thanks for your help.