Now that we have had a quick overview of Mongoose ODM and
how Mongoose brings schemas to structure the documents that we store in our MongoDB,
let's look at how we make use of
the Mongoose node module and then incorporate it into our node application.
Then use Mongoose to communicate with
our MongoDB server and then store and retrieve
documents with structure information in the documents.
To get started on this exercise,
go to a convenient location on your computer.
So, here I am in the Node JS folder where I have been storing
all the examples from this course and then I'm going to
create a new folder named node Mongoose.
This is where we will create the node application using Mongoose next.
In the terminal or the command window,
move to the node Mongoose folder and then initialize a node application there.
So, let me type npm init and then
initialize the node application and then I would name the application
as node-Mongoose and the entry point is index.js git history,
keywords, author and license and we'll say okay.
Once your node package.json file is created,
open the project in the text editor of your choice.
So, here I have my project open in Visual Studio.
Let me go into the package.json file and then add in
the startup script and then we'll
say start node index as usual whenever we start out with a new node application.
Then, going to the terminal or the command window, let's install Mongoose.
So, at the prompt type npm install Mongoose, save.
Which at the moment is Mongoose 5.1.7.
So, these are the versions that I'm going to be using in this course.
Then going to my application in the editor.
Let me create a sub folder in the project called as Models.
So, this is where I'm going to be storing all the models for my application.
In the models folder,
let me create a new file named dishes.js.
So, this is where I'm going to create the schema and the model
for my dishes document that I'm going to store,
the dishes collection which stores the documents for each dishes.
So, right there, let me first import Mongoose.
So, we'll require Mongoose in this file and then say
const schema is Mongoose schema.
Then, we'll create the schema here.
So, we'll say const dishSchema new Schema.
So, this is where I define the schema for my dish.
Inside this schema, let me define the various values.
So, I'll say name,
which is the type String.
So, this is the Schema type and then this I would declare as required.
So, I'll say true.
So, every document will have the name as a required field there and then I
will also declare this as unique meaning that
no two documents should have the same name field in there.
So, that is the first field in my document.
Then, we will also include another field called as, description.
The description is of the type string and required true.
Also, we can have Mongoose automatically insert
timestamps into our model.
So, right there we can just set up the flag time stamps: true.
So, this will automatically add the created at and updated at,
two timestamps into each document that is stored
in our application and it'll automatically update these values.
Whenever we update the document and the created at will be automatically
initialized when the document is first creator of this time.
After this, we will say var Dishes.
So, given the schema,
now we going to construct the model from this schema.
So, we'll say Mongoose model and dish,
and this is going to be using the dish schema that we have just declared earlier.
Then we will export
this model from this file here.
So, we'll say module export dishes.
So, now we have constructed
the Mongoose schema and the corresponding model and the model is now exported from here.
So, this can be imported in our application and used.
So, within our project folder,
let me create a new file named index.js.
In the index.js file,
let me first require Mongoose,
and then after this we can say
const Dishes require models dishes.
Because we have already created the Dishes model in
the models dishes file there and then we are requiring it here.
Now, to establish the connection to the Mongo server,
we'll say const URL mongodb.
Localhost 27017, conFusion.
So, this database is the one that we will connect to from our application and
then after that we can establish the connection by saying const connect Mongoose.
Mongoose supports this connect method,
which takes the URL as the first parameter.
So once we have established this,
then we simply say connect then.
Note that since we are already using promises,
we can just say connect then and then this will take
a function as the parameter
and inside here we can now connect to the database.
So now once this connection is established,
let me do a console log
saying connected correctly to the server,
and here I can create a new Dish by saying var
new Dish and then Dishes and inside here I can specify a document so I'm creating
a new Dish of this kind and then so in here I will
specify the two fields that are required.
So I'll say name,
description and so this is how you would create a new Dish and then once we create
a new Dish from the model we'll simply say new Dish save.
The save method will cause this Dish value to be
saved and then as you expect will return a promise and
then in here we will get the Dish value as call back in there and then,I
can simply log the Dish value here and then after that we'll say
Dishes find and so we'll find
all the Dishes and then say exec.
The exec will ensure that this is
executed and that it will return a promise and so that promise will
be returned so that it can then
chain the method to the remaining ones.
So you see how I am using promises and then I am invoking the previous method so this one
finds all the Dishes within my database,
in the Dishes collection and then makes it available to me.
So when I get the Dishes then I can just console log the Dishes just to see what is
returned and this obviously at this stage should
return this one single Dish that I've inserted into
my Dishes collection and then we will say
return Dishes remove with
an empty JavaScript object which will remove all the Dishes from
the database and return
mongoose connection close which closes the connection to the database.
And catch any error at this point.
So we'll use the catch and then console log.
That's it. So what we are doing here is we're creating the new this Dish and then we are
saving the Dish then we are finding all the Dishes from the collection.
That's it. We are using promises here,
so we have chained all the dens in here. That's it.
Let's save the changes and then see this application executing.
Go into to the terminal,
at the prompt type npm start and you will
see that my node application runs and then it shows,
first, that it has created this particular Dish.
Note in particular, that these two fields,
updated at and created at,
these two timestamps have automatically been added into
the Dish here and so you can see that the created at and
updated at timestamps are exactly the same at the moment and
then the ID is also automatically added in.
In the next step we are printing out all the Dishes that have been retrieved and so you
can see that this one single Dish is in the collection,
Dishes collection, and that is what is printed out on the screen here.
Let's now initialize the Git repository and then let's set up the got gitignore file
so going to the editor then we create the.gitignore file and then add
node modules to the gitignore file and save the changes and
then going back to the prompt let me
type git status and we see that
we have these files that have not been checked in so we'll say
git add and then git
commit the message Mongoose Part One.
With this we complete this exercise.
In this exercise we have seen how easy it is to set up a node application with
mongoose and connect to our MongoDB server and then interact with it.
Now since mongoose builds upon the MongoDB server,
Mongoose can access all the various methods
that are supported by the MongoDB driver also.