There are 20 documents in my collection, each having the same email address because I made 20 copies of the first document.
I have created a list of mock 20 emails.
I would like to iterate over the documents in my collection and assign each email in my list to the key “email” in each document.
Here is the structure of a document in my collection:
{
"_id": {
"$oid": "62a310f4e43c328922e6f7e2"
},
"Name": "Play Boi Roi",
"Email": "you@me.com",
"Password": "Booboo",
"Questions asked": {
"$numberInt": "25"
},
"Shared posts": {
"$numberInt": "66"
},
"Likes received": {
"$numberInt": "747"
},
"Medals": {
"Bronze": {
"$numberInt": "5"
},
"Silver": {
"$numberInt": "8"
},
"Gold": {
"$numberInt": "4"
}
},
"Current courses": ["Computer Systems", "Functional Programming", "Machine Learning", "Chemistry"]
}
I want to replace “you@me.com” with an email in my list e.g. [email1, email2, . . . , emailn]
Here is some code I found on MongoDB’s webpage. How do I update this code so it does the required task?
async function listDatabases(client){
databasesList = await client.db().admin().listDatabases();
console.log("Databases:");
databasesList.databases.forEach(db => console.log(` - ${db.name}`));
};
const {MongoClient} = require('mongodb');
async function main(){
/**
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
* See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
*/
const uri = "mongodb+srv://<username>:<password>@f<your-cluster-url>";
const client = new MongoClient(uri);
try {
// Connect to the MongoDB cluster
await client.connect();
const db = client.db("MyDatabase");
const coll = db.collection("MyCollection");
// Make the appropriate DB calls
await listDatabases(client);
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
main().catch(console.error);
//db.getCollectionNames();