0:03
In this exercise, we will continue with the previous exercise where we
developed the interaction between the node application and the MongoDB server.
In this exercise, I'm going to encapsulate
a few of the database operations into a node module of its own,
a file-based node module,
and then make use of it within myNODE application in order to interact with the server.
At the same time,
we'll perform several operations on the server to demonstrate that we will be able to
interact with the server using our node application and the node MongoDB driver.
To get started, let's go to
our project and then create a new file named Operations.js.
This file will encapsulate all that database operations,
the four operations that I'm going to perform, Insert,
Find, Remove and Update a document in my database.
Now, this will be organized as a file-based node module,
which then I will make use of it in myNODE application to access that server.
So, we get started,
let me first require
assert in this node module.
And since this happens to be a node module,
we'll be exporting several methods from the node module.
The first method would be insertDocument,
obviously, as you would expect.
And this will take four parameters, DB,
the MongoDB database connection within
myNODE application that I will obtain within the node application.
And then, the second one is the document which I want to insert.
The third parameter is the collection into which I want to insert that document.
And the last one is a callback function,
which will be called back once that operation is completed.
And then let's close the function here.
And this essentially encapsulates the insertDocument method here.
So, this is a function which is exported by this node module here.
Now, I'm going to also incorporate a few more methods here.
The second one would be findDocuments,
findDocuments, not just one but several documents.
And this, I would search the collection and
find all the documents that are in the collection.
So, that's why I'm only taking the database
and that collection as the two parameters along with the callback.
The third function that I'm going to implement and export from
here is the removeDocument.
This supports that delete operation.
And so the removeDocument will take the database, the document,
the collection and the callback as the four parameters,
and will call the callback when the operation is completed.
And the final one,
of course, is to update the document.
Now, of course, you don't necessarily have to do it this way.
I just felt that this will be another way of illustrating how you can
encapsulate the functions into its own node module.
This is just reorganizing the code in a way that is more easier to use.
So, for the exports -- for the updateDocument,
it takes the DB some way of identifying the document as the second parameter.
The third parameter is the update,
and the fourth parameter is the collection in which
this document exists and the callback.
So, four functions to be exported by the Operations.js file.
Now, within these functions,
let's implement one by one.
In the insertDocument, the first thing that I'm going to do is say
const collection and then say db collection.
And the parameter is the collection.
So, we'll look for that collection there.
Now, this has to be performed in all the four methods.
So, I'm going to go and paste this code into all the four methods
here because that's a function that I need in all of them.
So once I get hold of the collection, there,
as you recall from the previous exercise,
I can perform operations on the collection.
So let's say collection insert.
And this takes as that first parameter of the document to be inserted.
And the second parameter is the callback with the error and that result.
So now, when this document
is inserted by calling the insert method on the database collection,
the insert method, let me remind you,
is supported by the MongoDB driver.
So that's the method that we are using
here or that's the function that we are using here.
Now, the first thing that I'm going to check for is assert equal, err, null.
So, I want to make sure that I don't have an error.
So, I want to make sure that the error is null.
If it is not null,
then this will print out the information and then quit the application.
At this moment, I feel that this is okay to handle it that way.
Later on, we will see how we can have
a global way of handling all the errors when we implement,
in the next lesson,
another way of accessing the MongoDB database.
Now, after I do this,
I'm going to implement,
I'm going to simply log this information.
So, I'll say console log and inserted.
So, this information will be printed out to the screen here, inserted.
Now, again, remember these semi-colons and all that are important.
Don't forget them when you are typing in your code.
So I'll say inserted result.
This result object that is
returned will have on it a property called the result property,
and this result property will contain a value.
This result property is also a JavaScript object,
and this will contain a property n,
which tells us how many documents have been inserted.
So, that's the information that I'm going to print out here.
And then we'll go to the next line and say...
Documents into the collection.
Now this is just a way of informing the user that this operation took place correctly,
and then we will pass that result back to our calling function.
So we'll call the callback and then the result will be the parameter to the callback.
So when we implement the usage of this function in our index.js file,
we will be providing the callback there which will
receive the result as the incoming parameter.
So this is for the insert document.
Now for the find document what I'm going to do is to say, coll.find,
and I'm going to find all the documents,
so that's why I will give an empty JavaScript object in here which will match with
all the documents in the collection and then I'll say
toArray and this will take as
a parameter and here callback function,
and inside this callback function, of course,
I will assert that this is not null and
then we will say callback(docs).
We'll simply pass back the retrieved documents back to the calling function.
Now for the remove document,
I'm going to say, collection delete one.
So, I'll try to find the first document that
matches what we have specified and then delete it,
and then this will take, again,
a callback function as a second parameter.
And Inside this callback function,
the first thing that I check is to make sure that the error is not null.
And then after that we'll say,
console.log("Removed the document ", document).
10:18
We use the comma
here because this is a JavaScript object,
so if you specify console log like this the document will be printed out,
and then we'll pass that result back to the callback function.
And then finally, for
the update, we'll coll.updateOne.
This is a method the MongoDB driver supports,
so I'll say, update one document,
and the second parameter is where we will pass in
the fields that need to be updated and the way it is done is,
you'll say, "$set: update."
So this will take the update information that I'm
sending in and then pass it into the update one.
The first one is the document that needs to be updated,
the second one is which fields of the document need to be updated here,
and the third parameter is null.
And the last parameter is a callback function which,
obviously, will give us the result of the operation.
So first thing, I'm going to check to make sure that the error is not null.
Then I'll do a console.log("Updated the document with ").
Now again, the console logs are purely for us to
ensure that the code is doing what it is doing.
It doesn't help in any way.
This is just for our own information.
In a production server you would not be having these console logs anyway.
You can disable them. So that's it.
So four methods being supported in this particular node module, file based node module.
Insert, find, remove, and update.
So now that we have implemented this file based node module,
let's go to the index.js file,
and then to make use of the file based node module,
I need to require this here, so I'll say,
const dboper require, and since this is a file based node module,
I need to get the full path to the node module which in this case happens to
be dot slash operations because it is in the same folder as my index.js file.
Now once we have done this,
then right here, this function,
this code that we were doing to access the database now instead,
we will be using the dboper that we have just implemented to access the database.
So we'll say, dboper insert
document and this insert document takes the db as the first parameter.
The db here is this db that came in when we call MongoClient connect,
so that db will be passed in.
So that way my operations node module knows where to access the database.
And then the second parameter is the- so if you look at the insert document,
you will see that the second parameter is the document to be inserted,
so I will say, name.
I'm just going to construct a JSON object
or JavaScript object here which will
automatically be mapped into a JSON object when it is being inserted.
And the third parameter, as you see,
db document collection and callback,
so the third parameter is the collection and the collection is the dishes collection,
and the final one is the callback.
The callback again, as you recall, receives the result.
If you look back at the way we implemented the insert document,
the callback received the result as the parameter.
So inside this callback we will handle that result value here,
so when the result value comes in,
we'll do a console log.
We'll say, "Insert Document:\n" and we'll say, "result ops."
The ops tells you the number of insert operations that were carried out.
So this is another object that is going to be on the result,
JavaScript object that is passed back in as a parameter,
and so I'm just going to print out the value so that
will give us some information about what has happened there.
Now once this is completed,
inside this callback function,
I'm going to call the next database operation,
so I'll say, dboper.findDocuments,(db"dishes").
And the third parameter is docs which is the callback function.
And when I receive the docs,
I'm going to do it console.log saying
Found Documents and we'll
simply block the documents to the screen.
So this will print out the Found Documents.
Notice that this call is inside
the callback function that is applied for the earlier function call.
So that is something that I want you to notice specifically.
Now, again inside this function call,
now we need to do it this way because until this function,
this callback is called we cannot do the next operation.
So in the next operation,
I'm going to update the document that I have just inserted.
So I will say update the documents and let's say
updateDocument db and then the next parameter is the document.
And I don't need to specify the entire document,
I can only specify one field and then that'll find
the document that matches this particular field.
And what I'm going to do is the next parameter is the update that needs to be supplied.
So the update is which field I want to update.
So I'm going to update
the description field by saying Updated Test.
And then the fourth one is the collection which is dishes
and the final one is the callback function which obtains
the docs as the return value
or rather result as the return value.
The result of the operation that we just carried out,
the update operation that we just carried out.
And then inside this callback function,
I'm going to again do a
console.log saying Updated Document:\n.
And the updated document will be passed back in result.result,
on this property of the result object that is passed back in.
Again, notice how the calls are getting
nested inside the callback functions here,
I want you to notice this structure of the code specifically because
that is what I'm going to come back to in the next exercise.
Now, after I do that,
then I'm going to again find the documents.
So let me just copy that,
and then I'm going to use the same code here.
So I will say find the documents and inside here,
I'm again going to say Found Updated Document,
and then finally when I'm done to this I will simply call db.dropCollection.
So I'm going to delete this dishes collection so that I will clean up my database,
so that I don't have anything more.
Because for the next exercise I want to clean up
the database and then start with a cleaner database.
So I'm going to clean out that dishes collection and
this would result in a callback.
So inside here, I'm going to do a console.log saying
Dropped Collection and then I'll just print out the result that came in.
And then finally, close that database.
Note, that structure of the code maybe insertDocument and inside the callback function.
I'm going to call the next function,
and inside that, again inside the callback of that
I'm going to call the next function and inside the callback next function and so on.
So you see a nested set of callbacks here
and a tree-structured nested set of callbacks here.
That is something that I want you to pay attention to.
Let's save the changes and go and look at this version of our application.
Getting back to the terminal,
let me execute the application.
So let me type npm start and we'll see that result.
Now, from this result,
you can see that the document is inserted into the collection,
and this is the document that is inserted into a collection,
and that is the document that has been found.
In the second step,
we are finding that document.
So when we call the db find document,
so this is the document that is retrieved from my collection.
Then, I ask it to update the document with
this and then you notice that it says Updated Document,
and then here it prints the result or ops and it says n is equal to one,
number modified as one,
and it was okay.
And then it prints out the updated document when we find the updated document here.
And that is what is printed out here and note in
particular that the descriptions has been updated,
and then finally it dropped the collection.
That's it. So, we see how this application runs and is able to make use of
the node module that be implemented and then perform various database collections.
With this, we complete this exercise.
In this exercise, I have demonstrated to you how you would interact with
your MongoDB server from your node application.
We have implemented our own node module here,
and then used it within our node application by
encapsulating certain of the database operations into that node module.
This is a good time for you to do a Git comment with
the message Node MongoDB example part two.