-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongo DB Commands.txt
105 lines (103 loc) · 3.8 KB
/
Mongo DB Commands.txt
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
===================================Database Commands===================================
1. View all databases
show dbs
________________________________________________________________________________________
2. Create a new or switch databases
use dbName
________________________________________________________________________________________
3. View current Database
db
________________________________________________________________________________________
4. Delete Database
db.dropDatabase()
===================================Collection Commands===================================
1. Show Collections
show collections
________________________________________________________________________________________
2. Create a collection named 'comments'
db.createCollection('anyname')
________________________________________________________________________________________
3. Drop a collection named 'comments'
db.comments.drop()
===================================Row(Document) Commands===================================
1. Show all Rows in a Collection
db.comments.find()
________________________________________________________________________________________
2. Show all Rows in a Collection (Prettified)
db.comments.find().pretty()
________________________________________________________________________________________
3. Find the first row matching the object
db.comments.findOne({name: 'bhavya'})
________________________________________________________________________________________
4. Insert One Row
db.comments.insert
({
'name': 'bhavya',
'lang': 'JavaScript',
'member_since': 5
})
________________________________________________________________________________________
5. Insert many Rows
db.comments.insertMany
([{
'name': 'bhavya',
'lang': 'JavaScript',
'member_since': 5
},
{'name': 'ashish',
'lang': 'Python',
'member_since': 3
},
{'name': 'himanshu',
'lang': 'React',
'member_since': 4
}])
________________________________________________________________________________________
6. Search in a MongoDb Database
db.comments.find({lang:'Python'})
________________________________________________________________________________________
7. Limit the number of rows in output
db.comments.find().limit(2)
________________________________________________________________________________________
8. Count the number of rows in the output
db.comments.find().count()
________________________________________________________________________________________
9. Update a row
db.comments.updateOne
({
name: 'bhavya'},
{$set:
{name: 'bhavya',
lang: 'mongoDB'
}})
________________________________________________________________________________________
10. Insert a updated Row which is not present in the Data
db.comments.updateOne
({
name: 'bhavya22'},
{$set:
{name: 'bhavya22',
lang: 'mongoDB' }},
{upsert: true
})
________________________________________________________________________________________
11. Mongodb Increment Operator
db.comments.update({name: 'bhavya'},
{$inc:{
member_since: 2
}})
________________________________________________________________________________________
12. Mongodb Rename Operator
db.comments.update({name: 'bhavya'},
{$rename:{
member_since: 'member'
}})
________________________________________________________________________________________
13. Delete Row
db.comments.remove({name: 'Cyber'})
________________________________________________________________________________________
14. Less than/Greater than/ Less than or Eq/Greater than or Eq
db.comments.find({member_since: {$lt: 90}})
db.comments.find({member_since: {$lte: 90}})
db.comments.find({member_since: {$gt: 90}})
db.comments.find({member_since: {$gte: 90}})