Find Child's of Child document in Mongoose Node JS

This Article is basically to populate records/data/documents of child table and also for populate records/data/documents from Child's of child table in mongoose(Mongo DB).

Suppose, you have three tables

Questions schema is having the reference of Chapters (_id) and Chapters is having reference of Subjects (_id). So this is the Many to One relationship between them. 

If developer wants to populate Chapter name and Subject name with the Questions. Then you have to write find query like below:
Questions.find({})   .populate({       path: 'Chapters',      populate: {        path: 'Subjects',        model: 'Subjects'      }    })

Thanks for reading this article. Please share your comments on this article.

SaveOrUpdate method in Mongoose with Node JS

Hello Readers,
I am a Node JS developer. And developing a software on Node JS + Mongo DB. I was struggling in a situation where I need to save document in database and need to update documents if it is already present.

There was a record like below:
{ "name":"Dummy", "age":30, "phone":"000000000" }

There was a restriction like name is the UNIQUE field.

Conditions: If user is going to Insert the record with the same name, then old record should be updated.

Solution :

Student.update( { name : 'Dummy' }, { name : 'Dummy', age : 35 }, { upsert : true }, callback );

In the above solution, we used {upsert:ture}. So this will save the new record and update if it is already exist.

Thanks for reading this article. Kindly share your comments for this solution.

Use of req.query, req.params and req.body in NODE JS

I am writing this article for beginners. Because, at the time of learning node js, this is very confusing where and how we need to use req.query, req.params and req.body .
I am going to explain these three req method below.
1.Req.query 2.Req.params 3.Req.body
Req.query: When request is coming in key and value pair, for example, if your URL looks like below URL http://localhost:3000/search?name=xyz&age=16 In the above URL, you can see that request is coming in key and value pair. First key is name and value is xyz, second key is age and value is 16.
In the node js programing, if you want to parse this url then use req.query. Below is the code snippet to parse this URL.
router.get('/search', function(req, res, next) {   var name =;   console.log(`Name :  ${name}`)   var age = req.query.age;   console.log(`Age :  ${age}`)   res.send('Request has parsed see console');   });
Req.params: When request is coming like below URL. http://localhost:3000/search/xyz/16 in the above U…

Create database schema in NODE JS example

In this article, I will explain how to create database schema in Node js application. So as a database, I will use MongoDB, it is very compatible with Node JS because MongoDB stores data in JSON format. In this example, I will create a  schema of student’s detail for MongoDB
In action to create database schema, first create a folder models inside the application. This is the good practice to write all schemas in separate folder. Once you created models folder, create a student.js file. Steps: 1)Create a folder models in application. 2)Create Student.js file inside the models folder. 3)Copy and paste below code in Student.js file
const mongoose=require('mongoose'); const Schema= require('mongoose').Schema;
const studentSchema = mongoose.Schema({     name:{         type:String,         required:true,         unique:true     },     class:{         type:String,         required:true,         unique:false     },     section:{         type:String,         required:true,         unique:false     }…

Database Connection using NODE JS example

Database Connection using NODE JS example

In my earlier article, you have learned how to create your first node js application and how to create node js web application. If you have not read it, I am suggesting you to read these two articles. Below are the link of these articles.

After creating a web application, you need to deal with database. So before writing node js script, you need to download and install MongoDB in your PC. To download MongoDB go to the link. Before start writing node js database connection, please insure that MongoDB is running in your PC.
Now  open app.js file and write below configuration to connect with MongoDB.

const MongoClient = require('mongodb').MongoClient
var db
MongoClient.connect('mongodb://localhost:27017/', (err, database) => {

Generate NODE JS web application - Express generator module

Auto generate NODE JS web application folder structure.
This article is for beginners who wants to create a web application using NODE JS but don’t know about the project structure and basic files inside the folders.  You can create a folder Skelton using  Express generator module.
It’s very simple to generate web application using express-generator. Before auto generate project structure, you need to ensure that NODE JS is properly installed in you PC. Must read article to ensure that NODE JS framework is installed in your PC.
 You just need to follow below instruction to generate it.
1) Create a folder NodeJSExamples in your C:\ drive.
2) OPEN Command prompt and run cd NodeJSExamples command.
3) run $ npm install express-generator -g command to install express generator
4) run $ express --view=hbs mywebapp command. This command will generate the folder structure inside the mywebapp folder.

Below output will be gener…

Create First NODE JS application

Before creating the NODE JS application, please ensure that you have successfully install NODE JS server on your PC.

To download and setup the NODE JS, follow
After setup node js, check it is installed successfully using below steps. 1)Open command prompt.
2)Run node –v command
3)It should show current version of node js

Create your first project using below steps: 1)Open command prompt.
2)Create a new folder named “NodeJSApp”. Use md NodeJSApp command.
3)Run cd NodeJSApp.
4)    Run npm init command and follow instruction as below. Ensure you give app.js as a starting script file. This will create package.json file

5)Open this project in “Visual Studio Code” IDE.
6)  Create new file “app.js” inside the NODEJSAPP folder

7)Write below code inside the app.js

const express = require('express') const app = express()              const port = process.env.PORT || 4000
             app.listen(port, () => {
                  console.log(`Server started on port ${port}…

Recent Post

Recent Posts Widget