NoSQL: Creating Collections using MongoDB

In the latest article, we have used Mongoose to establish a connection to our database and we have also got a very nice overview of our environment and where all the components fit. Now, it’s time to make our project a little better by adding some data inside our database. And we will accomplish this task, of course, using Mongoose. As always, before diving straight into code I will propose some useful terms and their definitions in order for you to completely understand what we are trying to accomplish here.

So let’s start:

Just a bunch of students

As you may have guessed from the previous article, we are going to create a student collection. So this means that we will have a lot of data regarding students inside our app. Later, in this series, we are also going to make some tests on these data. But for now let’s start by specifying some useful terms with our first dictionary of this article:

Dictionary 1

  • Model: A Model defines a programming interface for interacting with the database. It represents all the records that sit inside of a collection. A model will also provide us with useful operations such as read, insert and update, all operations mentioned here.
  • Schema: A Schema represent the structure of a particular document. With Schemas, we are able to express what properties and values a document will expect, as well as constraints and indexes.
  • Instances: A model is also used to create objects that represents single records or single instances of that model.

To recap

To better explain the bullet points above, we can express it like this:

  • We will create a student model that will serve us as an interface with the database
  • This model will allow us to perform operations such as reading, writing and updating data
  • In order to create students, we will also have to give them a structure, this structure will be defined by a Schema
  • Thanks to model we will also be able to create single instances of Students. So if we have, for example, the student model, we will be able to create an instance of a student called ‘Mark’, who studies ‘Math’

Graphically:
model schema diagram

Little notes

Since I thought this was the best moment to do this, I would like to clarify again how important it is to start some planning before diving straight into code. 

From now on, we will start creating data, and these data will have a Schema. So please, when you will find yourself alone having to write an app using Mongo, stop for a second before writing any line of code and think properly about the data you need, how you want to represent those data, what constraints you will put on those data and how a collection will relate to others.

In my case I have decided to start with a very easy property for a student: the name. Later, we will add more info such as the class, the year of college, if he/she is doing any internship at the moment and so on.

Creating the Student Schema

Now that we have clear what we need to do in our mind, let’s start writing some code! Flip over to your text editor and create a new file inside of our src folder called student.js. In this folder we will define our Student Schema.

Let’s add some basic scaffolding to our project:
scaffolding project

If you remember from the previous article, we need to require every package before we can use it, and that’s what we are doing with mongoose. The Schema object is going to allow us to define a schema for the student model.

Defining the schema

Let’s now define the schema using the appropriate Schema object:
defining the schema object

As we mentioned before, we can accomplish this task using the Schema object and by defining what properties we want for our object. In our case we want just the name of the student. And that property is of course, a String.

Creating the model

Now that we have our Schema ready, we can create the Student model and feed it to Mongo using the following instruction:

creating the model code

This line is actually the most important of our program, here we have a couple of processes going on behind the scenes:

  • Mongoose asks Mongo if there is already a collection called student inside of it, if the answer is negative, then Mongo will create a that collection for us.(notice the student’ string to indicate the name of the new collection) 
  • Notice how we provide to the model() function the StudentSchema, that is done in order to specify to Mongo that whenever we create an instance of Student, we want it to follow that structure
  • The return value of this expression is assigned to the newly defined Student variable. This variable can be referred to as the Student model, and as you can guess it will represent the entire Student collection inside of our database. 

Exporting the model

The last part of the program is to provide the ability to other part of it to use the model we just created. To accomplish that, we will use this instruction:

export the model code

So now, with a simple require statement, you will be able to use the Student model in other parts of the program too!

Finals

With this article we accomplished a lot, we have created our first collection and got a deeper understanding of how things work in the MongoDB world. Here is the final version of the code:

full code demo

Feel free to copy & paste the code here –

const mongoose = require('mongoose'); 
const Schema = mongoose.Schema; 

const StudentSchema = new Schema({
    name : String
}); 

const Student = mongoose.model('student', StudentSchema); 

module.exports = Student;

Stay tuned for the next articles where we will go on in learning Mongo and will start exploring the world of testing using Mocha.

Interesting Links

Follow me on twitter @BorrelliDev or check out my blog thecoderswag.com. Stay tuned!

NoSQL: Creating Collections using MongoDB

You May Also Like

Leave a Reply

Your email address will not be published.