-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
45 lines (38 loc) · 1.24 KB
/
database.js
File metadata and controls
45 lines (38 loc) · 1.24 KB
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
const { MongoClient } = require("mongodb");
const url =
"mongodb+srv://hamzaakramkhan97:wrOe5956U9A8dnbK@nodejs.tfb8l.mongodb.net/?retryWrites=true&w=majority&appName=Nodejs";
const client = new MongoClient(url);
const dbName = "Firstdb";
async function main() {
await client.connect();
console.log("Connected successfully");
const db = client.db(dbName);
const collection = db.collection("User");
// Read document
const findResult = await collection.find({}).toArray();
console.log("Found documents =>", findResult);
const user = {
firstname: "Ali",
lastname: "Khan",
city: "Karahi",
country: "Pakistan",
phone: "923123123123",
};
// Insert document
// const insertResult = await collection.insertOne(user);
// console.log("Inserted documents =>", insertResult);
// return "done";
//update document
// const updateResult = await collection.updateOne(
// { firstname: "Ali" },
// { $set: { firstname: "Asad" } }
// );
// console.log("Updated documents =>", updateResult);
// Remove
const deleteResult = await collection.deleteMany({ firstname: "Asad" });
console.log("Deleted documents =>", deleteResult);
}
main()
.then(console.log)
.catch(console.error)
.finally(() => client.close());