forked from hafidzdev17/latihan-express-mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.js
28 lines (26 loc) · 941 Bytes
/
mongo.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
const MongoCLient = require("mongodb").MongoClient
// const connectionString = "mongodb://localhost:27017"; // tanpa authentication
const connectionString = "mongodb://user_latihan:123456@localhost:27017?authSource=admin";
// Cara 1
// MongoCLient.connect(connectionString, { useUnifiedTopology: true },
// (error, client) => {
// if (error) return console.error(error)
// console.log("Server database connect!")
// })
// Cara 2
(async () => {
try {
const client = await MongoCLient.connect(connectionString, {
useUnifiedTopology: true
})
// get db
const db = client.db("latihan")
// get collection table
const quote = await db.collection("quote").find().toArray()
const coba = await db.collection("coba").find().toArray()
console.table(quote);
console.table(coba);
} catch (error) {
console.log(error);
}
})();