-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathdatabase.js
More file actions
56 lines (42 loc) · 1.39 KB
/
Copy pathdatabase.js
File metadata and controls
56 lines (42 loc) · 1.39 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
46
47
48
49
50
51
52
53
54
55
56
const { MongoClient } = require("mongodb");
const url =
"mongodb+srv://namastedev:ECXK5j0d4OuHZhs6@namastenode.rc211ms.mongodb.net/";
const client = new MongoClient(url);
const dbName = "HelloWorld";
async function main() {
await client.connect();
console.log("Connected successfully to server");
const db = client.db(dbName);
const collection = db.collection("User");
const data = {
firstname: "Ranveer",
lastname: "Singh",
city: "Mumbai",
phoneNumber: "987543210",
};
const insertResult = await collection.insertOne(data);
console.log("Inserted documents =>", insertResult);
// Read
const findResult = await collection.find({}).toArray();
console.log("Found documents =>", findResult);
const countResult = await collection.countDocuments({});
console.log("Count of documents in the User collection =>", countResult);
// Find all documents with a filter of firstname: Deepika
const result = await collection.find({ firstname: "Deepika" }).count();
console.log("result => ", result);
return "done.";
}
main()
.then(console.log)
.catch(console.error)
.finally(() => client.close());
// NOTES
// Go to mongodb website
// Create a free M0 cluster
// Create a user
// Get the connection string
// Install Mongo DB compass
// Create a database
// INstall mongodb package
// Create a connection from code
// Documents CRUD - CReate, REad, Update, Delete