Skip to main content

Posts

Showing posts from July, 2022

Nodejs interview questions set 3

  1)What is nonblocking io? Ans:  Non-blocking I/O operations allow a single process to serve multiple requests at the same time. Instead of the process being blocked and waiting for I/O operations to complete, the I/O operations are delegated to the system, so that the process can execute the next piece of code 2)Async await in foreach? Ans: Array.prototype.forEach is not designed for asynchronous code. (It was not suitable for promises, and it is not suitable for async-await.) For example, the following forEach loop might not do what it appears to do: const players = await this.getWinners(); // BAD await players.forEach(async (player) => {   await givePrizeToPlayer(player); }); await sendEmailToAdmin('All prizes awarded'); What's wrong with it? The promises returned by the iterator function are not handled. So if one of them throws an error, the error won't be caught. (In Node 10, if no unhandledrejection listener has been registered, that will cause the process to

MongoDB interview questions set 1

  1)How to find the second highest salary ?   var records =[     { salary : 100 . ..m, 00 },     { salary : 9000 },     { salary : 9000 },     { salary : 5000 },     { salary : 10000 },     ]     let counts = await _db . collection ( "salary" ). insertMany ( records ) Ans: Solution 1 let highest = await _db . collection ( "salary" ). find (). sort ({ salary : - 1 }). limit ( 1 ). toArray (); let secondhighest = await _db . collection ( "salary" ). find ({ salary : { $lt :counts [ 0 ]. salary }}). sort ({ salary : - 1 }). limit ( 1 ). toArray (); Solution 2: step:1 var sal = await _db.collection("salary").distinct("salary") output:[   5000,   9000,   10000, ] step:2 9000 is the second-highest salary 2)How to find if the query for finding records uses an index or not?     let highest = await _db.collection("salaryy").find({itd:{$gte:0}}).sort({salary:-1}).explain('executionStats'); explain query will give al