0% found this document useful (0 votes)
14 views3 pages

Interview Questions NodeJS MongoDB

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
14 views3 pages

Interview Questions NodeJS MongoDB

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

Node.

js Interview Questions

1. What is Node.js, and how does it work?

Answer: Node.js is an open-source, cross-platform JavaScript runtime environment that allows

developers to build server-side applications in JavaScript.

It operates on a single-threaded, non-blocking, event-driven architecture, which is ideal for

handling concurrent connections without consuming many resources.

Node.js uses the V8 JavaScript engine, which compiles JavaScript to machine code, enhancing

performance.

2. Explain the role of the Event Loop in Node.js.

Answer: The Event Loop in Node.js is a mechanism that allows the server to handle multiple client

requests without creating a new thread for each one.

Instead, it uses an asynchronous, non-blocking approach, where tasks are added to an event

queue.

The Event Loop continuously monitors this queue and executes tasks in a single-threaded

manner, making it efficient for handling I/O-bound operations.

3. How do you create a simple HTTP server in Node.js?

Answer:

const http = require('http');

http.createServer((req, res) => {

res.writeHead(200, {'Content-Type': 'text/plain'});

res.end('Hello, World!');
}).listen(8081, () => {

console.log('Server running at http://127.0.0.1:8081/');

});

This creates an HTTP server that listens on port 8081 and responds with "Hello, World!" when

accessed.

4. What is npm in Node.js, and how do you initialize a project?

Answer: npm stands for Node Package Manager. It is the default package manager for Node.js

and helps manage dependencies.

To initialize a project, use npm init, which creates a package.json file containing metadata about

the project, such as its name, version, and dependencies.

MongoDB Interview Questions

1. What is MongoDB, and how is it different from traditional RDBMS?

Answer: MongoDB is an open-source, NoSQL database that stores data in a flexible, JSON-like

format called BSON.

Unlike traditional RDBMS, MongoDB is schema-less, meaning it doesn't enforce a strict structure

on stored data.

It uses collections instead of tables and documents instead of rows, which allows for storing data

with varying structures.

2. How do you create a database and collection in MongoDB?

Answer:

- Create a Database: Use the use command.

use myDatabase;
- Create a Collection: Collections are created automatically when inserting the first document.

Alternatively:

db.createCollection("myCollection");

3. Explain CRUD operations in MongoDB with examples.

Answer:

- Create:

db.collection.insertOne({ name: "John", age: 25 });

- Read:

db.collection.find({ name: "John" });

- Update:

db.collection.updateOne({ name: "John" }, { $set: { age: 26 } });

- Delete:

db.collection.deleteOne({ name: "John" });

4. What are indexes in MongoDB, and why are they important?

Answer: Indexes in MongoDB improve the speed of data retrieval operations by creating a

structure that allows the database to quickly locate data.

They are similar to indexes in relational databases and can significantly enhance query

performance.

However, excessive indexing can increase storage requirements and slow down write operations.

You might also like