-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16_MongoDB Syntax and Queries.txt
More file actions
129 lines (100 loc) · 6.4 KB
/
16_MongoDB Syntax and Queries.txt
File metadata and controls
129 lines (100 loc) · 6.4 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
->download: https://www.mongodb.com/try/download/community
->download mongodb shell: https://www.mongodb.com/try/download/shell
->Installation guide: https://www.geeksforgeeks.org/how-to-install-mongodb-on-windows/
or
https://youtu.be/tC49Nzm6SyM?si=QE07KNpCmxWHLrEc
-> Database is where we keep the data
-> MongoDB is an open source NoSQL database management program
->After installing and setup of MongoDB:
(Note: First learn basic things about SQL it will help a lot - optional )
->Let's focus on syntax and queries of MongoDB:
-There are two things : 1)MongoDB server and 2) MongoDB shell
-Commands to start server on Windows:
- for start : (net start MongoDB)
- for stop : (net stop MongoDB)
- Enter in DataBase
- Enter : (monogsh),
- show database : (show dbs)
->Show all databases: show dbs (it will show of list of databases)
->Create/use a database in mongodb : use myDb (here 'myDb' is name of the database, it can be anything )
- use myTestDb // Switch to or create the database
- db.createCollection("users") // Create a collection named 'users'
- db.users.insertOne({ name: "Muazim", age: 25}) // Add data
- db.users.find() // gives you data inside 'users' collection
Now to add another user inside users collection:
->first go inside users then > db.users.insertOne({name:"Basit",role:"Support Engineer",age:25})
Note: here after adding this data into users the mongodb will return this:
{
acknowledged: true, //means your data is inserted
insertedId: ObjectId('6815f148cea76829b2b5f89a') //unique id generated by MongoDB
}
-> now hit db.users.find(); you will see data inside users collection
->Finding Data:
Find All Users:
1)db.users.find() // Shows all documents in the users collection.
Find One User:
2)db.users.findOne({ name: "Muazim" }) //Returns the first match where name is "Muazim".
Find with a Condition:
3)db.users.find({ age: { $gt: 22 } }) // Users older than 22 (gt = greater-than.)
4)db.users.find({ age: { $lte: 24 } }) // Users age 24 or less (lte= less than equal)
Find Specific Fields Only
5)db.users.find({ name: "Muazim" }, { _id: 0, name: 1 }) //Returns only the name field (hides _id).
->Deleting data in users collection:
-> Delete One User:
(Delete the first user that matches a condition)
>db.users.deleteOne({ name: "Alice" })
-> Delete Multiple Users:
(Deletes all users where age is 25)
>db.users.deleteMany({ age: 25 })
-> Delete All Users:
(Deletes every document in the users collection)
>db.users.deleteMany({})
->Updating Data: (it takes two parameters: 1)which to document to update, 2)which field to update )
1)Update One Document:
>db.users.updateOne(
{ name: "Muazim" }, // Filter (which user to update)
{ $set: { age: 26 } } // Update (what to change)
)
2)Update Many Documents:
>db.users.updateMany(
{ age: { $lt: 23 } }, // Filter (users younger than 23)
{ $set: { status: "young" } } // Add or update `status` field
)
3)Add a New Field in existing document:
>db.users.updateOne(
{ name: "Ali" },
{ $set: { city: "Srinagar" } }
)
//Adds city: "Srinagar" to the user named "Ali".
4)Add New Field to All Users:
>db.users.updateMany({}, { $set: { active: true } })
->To exit enter this: >exit
Important:
->using the above commands via command prompt might be bit hard specially when the data is big
so to deal with that and to make it simple we can use a "MongoDB Compass GUI"
link to download: https://www.mongodb.com/try/download/compass
->if you don't want to use mongodb compass you can use "mongodb robo 3t" - download and use them
but mostly "mongodb compass is used"
->what it does is that: the databases which are present into your system it shows them graphically and you can also
interact with that data
->Important:
>After downloading and installing mongodb compass > open it
>After opening it click on Add New Connection:
->This compass also needs an server to run, basically what compass will do is that if your database is up it will
help you to interact with the data.
->how to find this url i mean on which mongodb is running
so when type this command > mongosh and press enter you will get the text and also this:
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.5.0
here this the url: mongodb://127.0.0.1:27017/ on this port its running
->copy and past this in browser and see what is the response
>So past this inside mongodb compass inside url field and click on save and connect button
>Now your data will be shown
>Now if you add any new data to mongodb (eg. via command promit) and then inside compass click on refresh that data
will be shown
>Now you can easily update the data in compass:
>click on edit icon on right side of the each document click on it to update the data and click on update button
>Now you can add new column new data via gui(compass as well) and then can see changes in database via command prompt
->Important:
Example we fill an form in frontend :
-Here the frontend is just arranging the data in a particular format(which is accepted in the backend/server) and then sends
this data to the server and then the server processes the data and send the data to the database