-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertMany.js
27 lines (26 loc) · 1022 Bytes
/
insertMany.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const { MongoClient } =require("mongodb");
// Replace the uri string with your MongoDB deployment's connection string.
const uri = "mongodb+srv://Monika:Monika5@cluster0.cjuq3d7.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0";
const client = new MongoClient(uri);
async function run() {
try {
// Get the database and collection on which to run the operation
const database = client.db("insertDB");
const foods = database.collection("foods");
// Create an array of documents to insert
const docs = [
{ name: "cake", healthy: false },
{ name: "lettuce", healthy: true },
{ name: "donut", healthy: false }
];
// Prevent additional documents from being inserted if one fails
const options = { ordered: true };
// Execute insert operation
const result = await foods.insertMany(docs, options);
// Print result
console.log(`${result.insertedCount} documents were inserted`);
} finally {
await client.close();
}
}
run().catch(console.dir);