How to Get Started with Mongoose ?
Learn basics about mongoose before jumping into it.
In this Article , I am going to touch basics that you should know before jumping to this Database framework.
- Why you should learn mongoose ?
- What mongoose will do ?
- What's the objective of mongoose ?
- How to get started along with basic commands ?
Why Mongoose ?
- One way of getting our Node.js app to work with the MongoDB database was through the use of the "native MongoDB driver." And You might have observed that how painful it was to do something very simple.(i.e. connecting MongoDB server to our Database)
- This is why most Node developers who are working with MongoDB use a package called Mongoose and this is what's called an ODM or an Object Document Mapper.
What Mongoose will do ?
- It will allow your Node.js app which speaks the language of JavaScript objects to be able to talk to your MongoDB database which speaks in the language of documents and collections and databases.
Objective
The main objective of this framework is to simplify:-
- The writing of validation code.
- The writing of your business logic boilerplate
- And just to make the code shorter and easier to work with.
- And essentially everything that you did previously using the MongoDB
driver, if we were to use Mongoose, all we have to write is pretty much just :-
const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/(nameOfYourDatab…, {useNewUrlParser: true, useUnifiedTopology: true});
Long back(if you had done front-end part) when you were working with the document object model, if you remember, it was quite a pain to write all of the code for the DOM. But, then You had started learning JQuery and adding those dollar signs everywhere, everything got a lot shorter and a lot simpler and easier to work with. And Actually Mongoose does the same job.
How to Start ?
Let me Name my DataBase as testDB
First things , make sure that you've got an active MongoDB server that's running.(else type command mongod
) in terminal.
And then if you open up a Mongo shell (i.e. use command mongo
in another new terminal) and type, show dbs
, you will see that you've got three pre defined database as admin, config and local already created and it's so small that it doesn't even really register a size.(0 GB)
Now we're ready to head back into our app.js
In our app.js, we now want to use this package called Mongoose.
But if you remember you haven't actually installed it using NPM yet.
So if you head over to terminal and make sure that you are still inside your project, then go ahead and use npm install to install the mongoose package. npm i mongoose
Once that's done then we can go ahead to require it in our projects app.js asconst mongoose = require('mongoose');
and then move on to the next goal which is to connect to our MongoDB database.
Now you can see at the moment in order to do that we're taking up something like 20 lines of code to get connected(using native MongoDB driver) and some of that will be validation code and some of it will just be the simple act of connecting to the database.
Now you can see how simple all of that becomes by using Mongoose instead.
So we'll write mongoose.connect(URL) and we're going to add the URL that we want to connect to. That connection URL is very similar to what we've got in mongoDB native driver. mongoose.connect("mongodb://localhost:27017/testDB")
and this will specify the port where we can access our MongoDB database server.
So it's going to make a connection to our MongoDB server and then it's going to look for a database called testDB , and if it doesn't exist, then it will create this brand new database. Just above line will replace all native mongoDB driver's code.
And now hit save and go over to your terminal and use node app.js
to run app, then you can see you've now started your server.(but sometimes you may get the deprecation warning due to bug of mongoose and for all of that just include {useNewUrlParser: true, useUnifiedTopology: true}
after connection URL).
After running you app , go to the mongo shell and type show dbs
and you will find that a your database gets added their along with initial three.
Now you are all set to test operations by reading the documentation from Mongoose Documentation.
The best way to learn is to implement by yourself !
- Hope you would love it.😊