-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (40 loc) · 1.23 KB
/
app.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const { MongoClient } = require("mongodb");
// Connection URI
const uri =
"mongodb://localhost:27017";
// Create a new MongoClient
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
async function run() {
try {
// Connect the client to the server
await client.connect();
// Establish and verify connection
await client.db("admin").command({ ping: 1 });
console.log("Connected successfully to server");
const myDB = client.db("fruitsDB");
//inserting many
const myColl = myDB.collection("fruit");
// const doc = [{ name: "Apple", color: "Red" },
// {name: "Orange", color: "orange" },
// {name: "Banana", color: "Yellow" }];
// const result = await myColl.insertMany(doc);
//inserting one
// const doc = { name: "Kiwi", color: "Green"}
// const result=await myColl.insertOne(doc);
// console.log(
// `A document was inserted with the _id: ${result.insertedId}`,
// );
//Finding
const findResult=await myColl.find();
for await (const doc of findResult) {
console.log(doc);
}
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);