Server-Side Scripting/Document Databases/Node.js (Express)

routes/lesson10.js
";   await client.close;    return result; }

async function getDocuments(collection) { return new Promise(function(resolve, reject) {      collection.find.toArray( function(err, documents) { if (err) reject(err); else resolve(documents); });   }); }

async function countryExists(country) { const client = new mongodb.MongoClient(HOST); await client.connect; const database = client.db(DATABASE); const collection = database.collection(COLLECTION); const filter = { country: country };   const count = await collection.countDocuments(filter); await client.close; return !!(count); }

async function insertCountry(country, temperature) { const client = new mongodb.MongoClient(HOST); await client.connect; const database = client.db(DATABASE); const collection = database.collection(COLLECTION); const document = { country: country, temperature: temperature };   await collection.insertOne(document); await client.close; }

async function updateCountry(country, temperature) { const client = new mongodb.MongoClient(HOST); await client.connect; const database = client.db(DATABASE); const collection = database.collection(COLLECTION); const filter = { country: country };   const update = { "$set": { "temperature": temperature } };   await collection.updateOne(filter, update); await client.close; }

async function deleteCountry(country) { const client = new mongodb.MongoClient(HOST); await client.connect; const database = client.db(DATABASE); const collection = database.collection(COLLECTION); const filter = { country: country };   await collection.deleteOne(filter); await client.close; }

module.exports = router;

Try It
See ../../Routes and Templates/Node.js (Express) to create a test environment.