8. Coding JavaScript in MongoDB
Activity 8.01: Creating a Simple Node.js Application
Solution:
Perform the following steps to complete the activity:
- Import the
readline
and MongoDB libraries:const readline = require('readline'); const MongoClient = require('mongodb').MongoClient;
- Create your
readline
interface:const interface = readline.createInterface({ Â Â Â Â input: process.stdin, Â Â Â Â output: process.stdout, });
- Declare any variables you will need:
const url = 'mongodb+srv://mike:password@myAtlas- fawxo.gcp.mongodb.net/test?retryWrites=true&w=majority'; const client = new MongoClient(url); const databaseName = "sample_mflix"; const collectionName = "movies";
- Create a function called
list
that will fetch the top five films for a given genre, returning theirtitle
,favourite
, andID
fields. You will need to ask for the category in this function. Look at thelogin
method...