0
0
Node.jsframework~10 mins

MongoDB connection with mongodb driver in Node.js - Interactive Code Practice

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

Complete the code to import the MongoClient class from the mongodb package.

Node.js
const { [1] } = require('mongodb');
Drag options to blanks, or click blank then click option'
ADatabase
Bconnect
CMongoClient
DMongoDB
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connect' instead of 'MongoClient'.
Trying to import 'MongoDB' which is not a class.
Using 'Database' which is not the correct import.
2fill in blank
medium

Complete the code to create a new MongoClient instance with the connection URI.

Node.js
const client = new MongoClient([1]);
Drag options to blanks, or click blank then click option'
A'mongodb://localhost:27017'
B'http://localhost:27017'
C'mongodb://127.0.0.1:3000'
D'ftp://localhost:27017'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http://' instead of 'mongodb://'.
Using wrong port number.
Using 'ftp://' which is not for MongoDB.
3fill in blank
hard

Fix the error in the async function to connect the client.

Node.js
async function connectDB() {
  await client.[1]();
}
Drag options to blanks, or click blank then click option'
Astart
BconnectDB
CconnectClient
Dconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'connectDB' which is the function name, not the method.
Using 'start' which does not exist.
Using 'connectClient' which is incorrect.
4fill in blank
hard

Fill both blanks to get a database named 'testdb' from the client.

Node.js
const db = client.[1]([2]);
Drag options to blanks, or click blank then click option'
Adb
Bdatabase
C'testdb'
D'mydb'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'database' instead of 'db'.
Using wrong database name like 'mydb'.
Not using quotes around the database name.
5fill in blank
hard

Fill all three blanks to get a collection named 'users' from the database and insert a document.

Node.js
const collection = db.[1]([2]);
await collection.[3]({ name: 'Alice', age: 25 });
Drag options to blanks, or click blank then click option'
Acollection
B'users'
CinsertOne
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'insert' instead of 'insertOne'.
Not using quotes around the collection name.
Using wrong method to get collection.