Now that we have learnt about the node MongoDB driver, the node module,
which enables our node application to be able to interact
with a MongoDB server, in this exercise,
we will use the node module and use the various methods that are
supported by the node module to provide introduction with our MongoDB server.
To get started, go to
the NodeJS folder that you have been using to store all your examples,
and then create a new folder there named node-mongo.
In this folder, we will develop
the NodeJS application that interacts with our MongoDB server.
Now, make sure that your MongoDB server is up and running,
so in the previous exercise we have already seen how our MongoDB server can be started.
So, keep the MongoDB server up and running and in
other terminal window or terminal tab or command window,
go to the node-mongo folder that you have just
created and initialize a node application here.
So, let me type npm init and we
will name the application as Node-Mongo and version,
description Node MongoDB Example,
and the entry point,
and the author, just like we have been filling in the previous exercises.
Then once the package.json file is initialized,
open this in the editor of your choice.
Now going into the package.json file,
let me introduce another script here just like we have been doing earlier, so,
we'll say "start": "node index" and save the changes.
Going back to the terminal,
let's install the MongoDB node module.
So, type npm install mongodb@3.0.10 --save.
We will also install the assert node module, so,
we'll say npm install assert@1.4.1 --save.
The assert module enables us to use the assert to check
for truth or false values within our application,
and you will see me using it later.
Now that we have installed these node modules,
let's create our node application that interacts with the MongoDB server.
Going back to the editor,
in the project folder,
create a file named index.js,
and this is where we will create
the node application that interacts with our MongoDB server.
So, in this application,
let's type const Mongoclient,
and then they'll say,
require( `mongodb`), the MongoDB node module that we have already installed,
and then this would provide us with
the MongoClient that enables us to connect to the MongoDB server.
Also, require the assert module.
Now, to start up a connection to the MongoDB server,
let's create a constant name URL which will be the URL where the MongoDB server can be
accessed and the baby access this is
by saying mongodb://localhost: 27017/.
Recall that this is the port number at which your MongoDB server is
running 27017 const dbname=,
and then they we'll say conFusion.
Recall that we created
the conFusion database in our MongoDB server in the previous exercise,
so, we're going to access that the conFusion database that we have created earlier.
Now, to access the server, we`ll say MongoClient.connect.
So, the connect method allows us to connect to the MongoClient from our MongoDB server,
and then we'll say this takes the URL as the first parameter,
the URL of the MongoDB server as the first parameter.
The second parameter is a callback function.
So, the callback function has two parameters, the error value,
and client, which we can then use
to connect to our database and then perform various operations.
So, inside this callback function,
first thing that we will do is to check to make sure that the error is not null.
So, the assert will check to see if error is equal to null,
so the assert function allows us to perform various checks on values.
So, we check to make sure that the error is not,
if it is not null,
then that means that there is an error and so we will show the error on the screen.
So, that's the use of the assert function here.
Now, if that doesn't happen,
then that means that we have connected properly to the server,
so we'll say `connected correctly to server`
const db= client.db(dbname) to connect to the database,
then we can say const collection db.,
so the db will provide us with various methods that enable us to
interact with the server so we'll say db.collection(` dishes`) So,
we'll try to access the dishes collection within this database.
Now, the next thing that we will try to do is
collection.insertone so we will try to insert one object into the collection,
and this object would be with
the name and description, "test".
So, this collection provides this method called insertOne,
which allows us to insert one document into this collection,
and the document is the first argument to this method here.
The second argument to the method is a callback function,
and the callback function takes two parameters, error and result.
So, within this callback function,
if the result is obtained,
then we are able to access the collection and then perform further operations.
So, here, the first thing that we will do is check to make sure that error is not null.
So, we'll do an assertion there,
and then here we can say, "After Insert".
So, after we insert, then we see that we
get the result value that is returned there, so this result.
This result will also provide
this OPS property which says how many operations have just been carried out successfully.
So, that is what I am going to print out.
So, it will say number inserted is one.
So, which means that this particular document has been inserted into the database.
So, that's what we will confirm here.
Then, after that, we will perform another option,
an operation on the collection, and say,
collection find, and we'll try to search for all the records in the collection.
So, here, we will supply an empty JSON string or corresponding level JavaScript object.
So, it will search for everything that is there in the collection,
and then, provide that to us.
Then, this can be converted to an array of JSON objects.
So, this is why we call the toArray,
and this takes a callback function as the parameter.
The first parameter of course is the error value, so again,
we check to make sure that the error is not null.
Then we'll say, console.log found,
and then print out whatever that has been found,
and we'll say console.log.
So, the second parameter here, docs,
will return all the documents from this collection
that match whatever criteria that you specify here.
Since this is empty,
then that means that all the documents in the collection will be returned to us.
You can specify a filter here,
saying name is equal to a value,
and then only those documents that match that value will be retrieved.
So here, let me just print out the docs here,
to indicate that this item has
been inserted into the collection and then will be retrieved,
and then we'll just print it out just to make sure that the document that we just
inserted in the previous operation is indeed in the collection,
and so we'll print that out here.
Now after this, then we will use
the dropCollection method here.
So, the dropCollection will drop the specified collection here.
So, we'll say dropCollection dishes,
so I'm just going to remove the dishes collection from my database.
So, I am just going to clean up the database and then leave
it with an empty database so that we can go onto
the next exercise where we can again
perform similar kind of operations to check this out.
This one takes, as a second parameter,
a callback function which returns either an error or a result.
Inside this, we will check and make sure that the assert is not null,
and then we will say, client.close.
So we will close the connection to the database on the spot.
So with this, this simple set of methods enable us to interact with our server,
and also it demonstrates to us how we can communicate with the server using
the methods that are available through this MongoDB node module or the MongoDB driver.
So, here, you see that first we perform an insert operation.
Then after the insert operation, note that,
we are doing the second operation inside the callback function.
So, this ensures that this operation has been completed before you do the next one.
So, this is a second call which is enclosed inside the first callback function,
and then correspondingly, inside of that.
We are again calling the methods here and inside there.
So, you see that there is a nesting of these calls,
one inside the other.
Note the structure because I'm going to come back to
explain something about this in one of the later exercises.
So, note that, nesting of these calls one inside the other.
Let's save the changes,
and then see what this application does when we execute it.
Going back to the terminal, again,
make sure that your MongoDB server is up and running, and so,
when it is running it is going to print out a lot of log messages onto the console.
We can just ignore those log messages for now.
Let's run the node application.
So, we'll say, npm start,
and when the node application runs,
you notice what does node application is doing.
So it says, connected correctly to the server, After Insert,
then it shows that
this particular document has been inserted into our MongoDB and the node in particular,
the ID given here.
So, notice that this doesn't say this is a object ID.
This ID is automatically inserted by
the node Mongo driver into their document when
you try to insert it into the MongoDB server here.
This format is exactly the same as the object ID that we have seen.
Thereafter, we performed a find operation.
So, this is found,
and then it prints out this item here.
Note that from the previous exercise,
we had left over in earlier document that we had inserted in the previous exercise.
So, that is still seen in the collection here,
and the new document that we just inserted here.
This document is seen right below that here,
but note that these two have two different IDs.
This ID matches the ID of the document that we just inserted.
So, this is leftover from the previous exercise.
After this, of course we drop the collection so
this whole set of documents and the dishes collection will be removed from our database.
So, now our database will be clean and empty.
So, that demonstrates to you how we can use
the MongoDB driver to communicate from our node application with the server.
Now that we have our application,
let me introduce a.gitignore
here and then put node modules into the.gitignore,
and you quickly understand why I'm doing this.
Then, we will go back and initialize the Git repository and then do a git commit.
Going back to the terminal,
let me initialize the Git repository,
and then, let's check the Git status and you see that there are some uncommitted files.
So let me check them in and then do a git commit with the message,
Node MongoDB Example one.
This is the part one of the exercise,
so we'll just check the items into our git repository.
This completes this exercise.
In this exercise, we have seen how we install the node module,
the MongoDB driver, then,
we were able to communicate with our MongoDB server from within
our node application using the methods that are provided by the MongoDB driver.
We will continue with this exercise in the next part of
the exercise where we will explore a few more methods of the MongoDB driver.