0
0
Node.jsframework~10 mins

Why database connectivity matters in Node.js - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the database client module.

Node.js
const dbClient = require('[1]');
Drag options to blanks, or click blank then click option'
Aexpress
Bmongodb
Cfs
Dhttp
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' or 'http' which are for servers, not databases.
2fill in blank
medium

Complete the code to connect to the database using the client.

Node.js
dbClient.connect('[1]', (err) => {
  if (err) throw err;
  console.log('Connected!');
});
Drag options to blanks, or click blank then click option'
Amongodb://localhost:27017
Bhttp://localhost:3000
Cftp://localhost
Dfile://localhost
Attempts:
3 left
💡 Hint
Common Mistakes
Using HTTP or FTP protocols which are not for database connections.
3fill in blank
hard

Fix the error in the code to properly close the database connection.

Node.js
dbClient.[1]();
Drag options to blanks, or click blank then click option'
Astop
Bdisconnect
Cend
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'disconnect' or 'end' which are not valid methods for MongoDB client.
4fill in blank
hard

Fill both blanks to create a query that finds all users older than 18.

Node.js
db.collection('users').find({ age: { [1]: [2] } }).toArray((err, results) => {
  if (err) throw err;
  console.log(results);
});
Drag options to blanks, or click blank then click option'
A$gt
B18
C$lt
D21
Attempts:
3 left
💡 Hint
Common Mistakes
Using '$lt' which means 'less than', or wrong age values.
5fill in blank
hard

Fill all three blanks to insert a new user with name and age into the database.

Node.js
db.collection('[1]').insertOne({ name: '[2]', age: [3] }, (err, res) => {
  if (err) throw err;
  console.log('User added');
});
Drag options to blanks, or click blank then click option'
Ausers
BAlice
C30
Dproducts
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong collection names or putting age as a string.