References vs multiple collections


I’m a bit new to Meteor and NoSQL in general. I was curious what the best practice is for handling one-to-many data where I want to store a unique index such as an ID? In this case is it better to create multiple collections to keep the unique IDs separate, or come up with some kind of unique index name based on the parent object and the unique id I want to track?

For example, let’s say I had a site to manage garage sales and each garage sale has a collection of items that I want to uniquely number. Each item only belongs to one garage sale and no two items can have the same item number. However other garage sales can have items using the same numbers.

I want to use a unique index to make sure that concurrent inserts can’t create the same unique item number, but how do I do this so that I can uniquely index based on some parent? Is a per-garage sale item collection the best way to go, or is there some way to create a unique index that include the parent item reference?


I could be misunderstanding your question, but I think you could just simply include a reference back to the garage sale in your garage sale item collection, just using the unique ID that is generated by Mongo DB. So I believe what your are looking for could be accomplished with a reference and multiple collections are not necessary.

For example:

GarageSales = new Mongo.Collection('garage_sales');
GarageSaleItems = new Mongo.Collection('garage_sale_items');

  addItemToGarageSale: function(garageSaleItemAttributes, garageSaleId) {  
            var garageSaleItem = _.extend(garageSaleItemAttributes, {      
                garageSaleID: garageSaleID


On the client you would do something like:'addItemToGarageSale', garageSaleItem, garageSale._id, function(error, result) {

Alternatively you could add the garage sale id of the current garage sale into the garage sale item on the client instead of passing it in to the meteor method separately.