Using a collection that already exists in Meteor

How do you access a Meteor collection that already exists? This is easy enough when you created the collection in session because you have a variable that refers to it, but you cannot access the collection by name.

What happens if, for example, you want to get documents from an existing collection in a new session where it is not created the first time. I tried to "recreate" it, hoping that it would just assign the existing collection to a new variable name (seeing as I can't find it by name), but it just throws an error to tell you the collection already exists.

+3


source to share


1 answer


I have externally generated collections that I get through a meteorite. I'm not 100% sure that this will answer your question, but I hope it at least helps.

One of them (not relevant to you, seems to be here for completeness) is that if your collection was not Meteor generated, you will need to export the environment variable to the Meteor point to your DB. For example, if the following env. the variable is exported to the shell:

MONGO_URL=mongodb://localhost:3002/foo

      

... and then you call the meteor app, it will point to the db "foo" in MongoDb, after which you just define your collections as @Akshat mentioned above in his comment:

collection = new Meteor.Collection("fooCollection") // this lives inside the foo DB.

      



If you are dealing with collections that were created by Meteor, by default they will be inside the meteor db, for example:

MONGO_URL=mongodb://localhost:3002/meteor

      

... and you can just plug them in the same way; just by declaring your collection and using it the way you are. No need to create, obviously.

It looks like you are already doing this, but for other newbies like me: in such cases it is very convenient to use the console in Chrome, Firefox, etc. and doing some inserts this way - you will see where the data lands, or you will see other good bits of information to help you get home to the problem - console.log () saved my bacon several times.

Anyway, it's worth checking exactly where the Meteor app points, and where you think it points. Your collections should be accessible and should just work ...

0


source







All Articles