Question about file structure -- Mongo.Collection();


I just got done doing the simple-todo tutorial and I am now moving the files into their own folders/files based on everything is easy to follow except one and that is where do you usually place Tasks = new Mongo.Collection('tasks');?


In my projects collections usually end up in “lib” folder, or mby in “both” now when there is some stuff to put there for SSR.


All JavaScript files outside special directories are loaded on both the client and the server. That’s the place for model definitions and other functions. Meteor provides the variables Meteor.isClient and Meteor.isServer so that your code can alter its behavior depending on whether it’s running on the client or the server.
- ( Meteor API Documentation )

This means you can place these where you want them, unrestricted by Meteor. As @shock mentioned, you can use the foldernames lib, both or your-custom-folder, if you want the collection to be accessible from the client (minimongo) and the server (mongodb).


To add to the existing answers, I place them under a globally defined var to reduce polluting my global namespace.

## _.js // for file load order
db = {};

## tasks.js
db.Tasks = new Mongo.Collection('tasks');
db.Tasks.attachSchema(...); // attach schema
db.Tasks.helpers({ ... }) // attach collection helpers
db.Tasks.before.insert(function () {...}) // attach collection hooks

I put the schema, helpers and hooks all in the same file to keep all collection related logic together each collection.


Interesting suggestions. Thanks guys. I’m following method 1 for organizing y project. Where do you guys usually dump your routes?

My structure looks like this…


Not sure if this is the standard way or not.


I only speak for myself, but since I started moving all collection logic into one file, I started doing the same for route/template/helper/events logic too so now my ‘page’ file looks like this:

## taskList.js
Router.route('taskList', ...)

//if you use autoforms

// template logic


I used to have a separate file just for routes, one for hooks but that meant i was jumping all over the place for template logic. Now I just have my jade template, my template logic and my collection/schema open.

This will help if you ever want to modularize into packages in the distant distant future… :slight_smile: but let’s not create more problems just now


Not sure if this is the standard way or not.

In Meteor, there is no standard. However, where you place files and how you name them, can affect the load order. I know, it feels weird at first…