How to add Collections to a Collection


I hope someone can help me.

I’m building an app that has categories and every category is a Collection, like: shoes, accessories, tops etc…
I have one collection called “categories”. I would like to add my category Collections to the collection “categories” so I can iterate over it with an {#each} loop.

Thank you

Let’s clarify some terminology here, specifically for the Meteor & MongoDB context.

A collection in this context is a MongoDB collection that is published & kept in sync from server to client via Meteor’s DDP.

A list of things.

The latter can also be called a “collection” (e.g. when coming from a Java background) but in the Meteor context that would cause confusion and so we just call it array or list or set or something else, but not “collection”.


What I understand is that you have a Meteor/MongoDB collection called “categories”, and every category (aka entry in that collection) has a property that is a list of things which you also called “category”/“categories”.
Well, right there you already seem to have a problem with your model, the structure of your data. And from there follows naturally that you’ll also have trouble expressing that ill-defined model in the context of Meteor, as you would have in any other programming context/environment.

Questions I would ask myself if I were in your place:

  1. What other properties or attributes do the items in my MongoDB collection “categories” have?
  2. What real-world scenario am I trying to express with my data modelling here? Give a specific example of the functionality / feature you are trying to create using the data model.

From there it should quickly become much clearer what you actually need, how to name things, how to structure the data model and then your apparent “I’m having trouble implementing this in Meteor” is going to go away.

And maybe I can speak about an implementation of the simplest case that might match what you need:

Usually a site, let’s say it’s an ecommerce site with products on it, will have a list of (product) categories that are predefined and can be used to categorize products with. Which could be expressed in the data model like this:

  _id: String
  slug: String (url-friendly)

  categories: Array of String (each a category._id)

Then you would on the server Meteor.publish() all the categories collection items, and on the client have a Meteor.subscribe() and a Template helper set up that returns all category-s via Categories.find() and thus you can iterate over all of them using {{#each categories}}, for example when displaying the ecommerce site’s category-based navigation menu.

1 Like

Hey thanks for helping me out so extensively.

I’m not quite sure if you understand me or if I understand you.

So let’s discuss the data model maybe I’m doing it all wrong.

The goal is to have an ecommerce application. With categories like: shoes, accessories and tops.
In the end I want to display a category that holds any number of products. And I want to display a list with all the categories.

Based on what I think and what you wrote I get to this:

  _id: String
  slug: String
  title: String

  categories: Arrray of String (each a category._id)

  _id: String
  slug: String
  title: String

  brand: Array of String (each a brand._id)

  _id: String
  slug: String
  title: String
  category: category._id
  brand: brand._id
  likes: Number

Is this what you mean? If so, I get it. :slight_smile: and it’s exactly as how I pictured it.


I’m writing this to describe what I ended up doing and to give a satisfying answer to my own question. It may hopefully be helpful to someone some day.

I just wanted two lists one with categories and one with brands.

So what I ended up doing is using an Array:

	brands: [
	{ name: "Nike"},
	{ name: "Adidas"},
	{ name: "Converse"},

and iterate over that in the template with an {{#each}} loop.

At first I thought I needed to have individual collections for brands and categories. But after reading this post
I learned that

Your first instinct should be to place as much in a single document as you can.
And query the ‘document’. So that’s what I did. I created two more fields in the product object to add a category and a brand to a product. So no reference _id to another collection.