Associations

photo-1514517521153-1be72277b32f

I might as well just start in the middle for now! I will include stories and memories from learning as I go – but I think it will be better for me to start while it’s all fresh in my mind.

I’m currently working on building a small web application to track milk production for specific animals on the farm. This is requiring me to use everything I’ve learned and then some.

I’m using the MEAN stack to write it (because that’s what I’ve learned so far). MEAN stands for:

– MongoDB
– Express
– AngularJS
– Node.js

At this point in time I am not using Angular.

I’ve got the project decently far using the knwoledge that I have but I’m jumping back into the course work for a bit to fill in the gaps – so I can clean things up a bit.

In the course we are learning associations – this is an area I struggled with on the Milk Log App. I didn’t really know how it was supposed to be done so I just created a nested array inside my animal schema.

var animalSchema = new mongoose.Schema({
    image: String,
    name: String,
    variety: String,
    DOB: Date,
    logs: [{
        amount: Number,
        notes: String,
        dateMilked: Date
    }],
    created: { type: Date, default: Date.now }
});

I breifly entertained the idea of having two databases, one for animals and one for logs. But it seemed like that would be a bit more tricky. I’ve managed to get all my RESTful routes working for both animals and logs – YES! – It took me about three days and I spent a lot of time on stack overflow. But it works.

Now I’m going to write about learning the other ways I could have done it!

The first method we went over is embeded association, which is pretty close to how I did it but instead of giving the main item an empty array we defined a schema for the second item and put the name of that schema in the empty array. The following is an example of a blog creator and their posts.

//POST - title, content
var postSchema = new mongoose.Schema({
    title: String,
    content: String
});

//USER - email, name
var userSchema = new mongoose.Schema({
    email: String,
    name: String,
    posts: [postSchema]
});

We then covered referenced associations, which means that the array in the blog user item has references to post items. This was the method I had orginally intended to use, but I had no idea how to go about it. Now I know for next time. You can see the difference in the schema below:

var postSchema = new mongoose.Schema({
    title: String,
    content: String
});

var Post = mongoose.model("Post", postSchema);

var userSchema = new mongoose.Schema({
    email: String,
    name: String,
    posts: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Post"
    }]
});

Then when you need to access the array, and you want more than just the object ideas you can use .populate :

User.findOne({ email: "bob@gmail.com" }).populate("posts").exec(function(err, user) {
    if (err) {
        console.log(err);
    }
    else {
        console.log(user);
    }
});

Which will then allow to you pass that array to a webpage to be used, or do whatever you would like with it.

That’s all for this lesson, but we will be going into more detail about why we might use one method over the other as we work on the Yelp Camp Application.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: