Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases Mongo Basics Working With Collections Managing Collections

How to have more tiers in the MongoDB hierarchy

Hi,

I am a newbie in MongoDB. My understanding is that to retrieve a document we use db.collection.find() so the hierarchy is db -> collection -> document.

However if I want to have more tiers how do I design my database. For instance, I can create a db for each 'client', a collection for each 'project', but underneath 'project', I want to have another 'campaign' collection and maybe more tiers to actually store my documents.

I am not sure if I explained my question properly but any input is appreciated!

Thanks, Gary

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Gary;

I have not seen that design pattern used in a Mongo database. I think more common would be to store the campaign details within the customer document something along the lines of:

{
    user_id: ObjectId("4c4b1476987d9c8dd6000000"),
    customer_first_name: "John",
    customer_last_name: "Smith",
    campaigns : [
        {
             campaign_id: "A1",
             campaign_joined: ISODate("2016-10-19T06:30:32Z")
        },
        {
             campaign_id: "B2",
             campaign_joined: ISODate("2014-12-31T15:18:32Z")
        }
    ]
}

Or whatever details you want stored on the customers and campaigns. If you wanted to store all of the campaign information in a separate collection and use the associated campaign_id as a relationship between collections, that would be a popular design pattern.

Alternatively, if you want to embed the customer information inside each campaign, you could do that too. Something like:

{
    campaign_id: ObjectId("4c4b1234567b3d2dd5000001"),
    campaign_name: "A1",
    customers: [
        {
         customer: "John Smith",
         email: "john@smith.com"
        }
    ]
}

Then your queries would be designed around drilling down into each campaign to see if a specific customer was involved in a specific campaign. So, for example:

db.campaigns.find({'customers': {$elemMatch: {'customer': 'John Smith'}}})

Does that answer the question?

Post back with additional questions,
Ken

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Laug;

I'm not sure I fully understand what you are wanting to do, especially as it relates to the design of a MongoDB database.

Each database can have multiple collections and each collection can have multiple documents. If you could explain a little more about how you are envisioning your data to be used, and for what purpose, it might help to assist you with how it should be designed.

Post back with further questions or comments.
Ken

Thanks Ken! I am designing a database to store all my clients' campaign/promotion event details.

Let's say client A has 3 major projects, P1, P2 and P3. And under each project there are several campaigns such C1, C2, etc. And under each campaign there are customers who were involved.

So if I want to retrieve one particular customer details, I would need to:

use A;  # which is the db name
var campaign = db.P1.find({campaign: C1});  # which returns the campaign object
var customer = campaign.findOne({customer: "John Smith"}); # not sure if I can do this but this is what I need

What I want to know if whether there is a better way to chain these steps. i.e.

use A;
var customer = db.P1.C1.findOne({customer: "John Smith"}); 

Is this possible?

Thanks Ken! Having the campaign in a separated collection is a good idea!