Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the database client module.
Node.js
const dbClient = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' or 'http' which are for servers, not databases.
✗ Incorrect
The mongodb module is used to connect to a MongoDB database in Node.js.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using HTTP or FTP protocols which are not for database connections.
✗ Incorrect
The connection string for MongoDB starts with mongodb:// followed by the host and port.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'disconnect' or 'end' which are not valid methods for MongoDB client.
✗ Incorrect
The correct method to close a MongoDB client connection is close().
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '$lt' which means 'less than', or wrong age values.
✗ Incorrect
The MongoDB query operator $gt means 'greater than'. We want users older than 18.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong collection names or putting age as a string.
✗ Incorrect
We insert into the 'users' collection a user named 'Alice' who is 30 years old.