What is a good method to insert multiple documents into mongodb?
I have to perform two inserts (into 2 collections) - "n" times.
Currently, I accomplish it with a while loop but it seems like a lot of requests to the database.
handler.post(async (req, res) => {
while (potentialMatchCount < 10) {
potentialMatchCount+=1;
const email = gender=="0"?"m":"f" + potentialMatchCount + "@wedpicker.com"
const matchId = nanoid(10);
await req.db
.collection('users')
.insertOne({
_id: matchId,
email,
});
await req.db.collection('userLocations').insertOne(
{
"_id": matchId,
},
{
$set: {
type : "Point",
coordinates : matchLocation,
updated: new Date(),
},
}
);
}