-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongonotes.txt
executable file
·143 lines (89 loc) · 4.76 KB
/
mongonotes.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
TO SHOW DBs show dbs
use {name} -(creates, or accesses if already exists, a db)
show collections - tables inside dbs
db.dropDatabase() - delete database
exit - exit terminal
//INSERT SINGLE ENTRY
db.users.InsertOne({obj}) - db refs the active db, users (creates collection/table users), InsertOne (inserts one record within table)
//insert takes in an obj, JS like syntax
db.users.find() - gets all values within database
//ids are auto generated
dbs ---> collections ----> documents (i.e. objects)
Can nest objects, arrays et al within docs ala JSON literal
//INSER MULTIPLE ENTRIES
db.users.insertMany([{}])
Like insert one except you pass in an array of objects
//QUERYING - READ INFO from DBs
db.users.find().(method())
find has multiple methods
db.users.find().limit(3) //limits result to the specified number
db.users.find().sort({name:1}) //sorts in ascending (1) or descending (-1) order
db.users.find().sort({name:1, age:-1}) //can pass in multiple args to sort
db.users.find().skip(1).limit(4) //skip 'skips' the given amount
//WHERE name ==
db.users.find({name:"T"}) //returns where name == "T", type specific
//SELECT name where ==
db.users.find({name:"T"}, {name:1, age:1})
second obj arg specifies which fields to return
To Omit (default ID) pass { _id:0 } as an arg within second obj
Passing second arg as 0 omits that field, can be stacked with other ommissions, can't be mixed
//db.users.find({name:"T"},{ hobbies:0, age:0})
1 select field, 0 select everything but this field
//COMPLEX QUERIES
db.users.find({name:{$eq:"Yonas"}}) - return where name === "Yonas"
db.users.find({name:{$ne:"Yonas"}}) - return where name != "Yonas"
db.users.find({age:{$gt:31}}) - return where age > 31
db.users.find({age:{$gte:31}}) - return where age >= 31
db.users.find({age:{$lt:31}}), db.users.find({age:{$lte:31}}) //less than or eq to
db.users.find({name:{$in:["T", "Yonas"]}}) - if the name attr is in this list of names return INSERT
db.users.find({name:{$nin:["T", "Yonas"]}}) - if the name is not in this list of names
db.users.find({age:{$exists: true}}) - return objects where age key exists
db.users.find({age:{$exists: false}}) - return objects where age key doesn't exists
///////////////////////////////////////////////////////////////////////////////////
//AND QUERY
db.users.find({age:{$gte:20 , $lte:40}}) - return obj where age is >= 20 && <= 40
db.users.find({age:{$gte:20 , $lte:40}, name: "T"}) where name is T and age considerations
alt to above
db.users.find({$and:[{age:{$lt:31}}, {name:{$in:["T", "Arnold"]}}]})
$and takes in an array of objs
//OR
db.users.find({$or:[{age:{$gt:31}}, {name:{$in:["T", "Arnold"]}}]})
//NOT
db.users.find({age:{$not:{$lte:32}}}) - returns all objs where the age is !< 32 inc objs where key doesn't exists
//COMPARE KEY VALUES between Objects eg balance >>> debt $expr
db.users.find({$expr:{$gt:["$debt", "$balance"]}}) - returns where debt >>> balance
//NESTED FIELDS
db.users.find({"address.street":"123 Street"})
//FIND ONE
db.users.findOne({age:{$lte:32}})
//COUNT
db.users.countDocuments({age:{$lte:32}})
db.users.countDocuments({age:{$lte:32}})
///////////////////////////////////////////////////////////////////////////////////
UPDATING
//UPDATING & ADDING PROPERTIES
db.users.updateOne({name:"T"},{$set:{age:36}}) - updates where name is T, alternatively
db.users.updateOne({_id: ObjectId("633315153463fb5bf6ed65a2")},{$set:{name:"Tolu"}})
//UNSET/REMOVE a property
db.users.updateOne({_id: ObjectId("633315153463fb5bf6ed65a2")},{$unset:{age:""}}) - removes age property
//INCREMENT VALUES
db.users.updateOne({_id: ObjectId("633315153463fb5bf6ed65a2")},{$inc:{age:2}}) - increases age value by 2
use negative values to decrement
//RENAME KEYS
db.users.updateOne({_id: ObjectId("633315153463fb5bf6ed65a2")},{$rename:{name:"firstName"}})
//ADD to ARRAYS
db.users.updateOne({_id: ObjectId("633315153463fb5bf6ed65a2")},{$push:{hobbies:"coding"}})
//REMOVE FROM ARRAYS
db.users.updateOne({_id: ObjectId("633315153463fb5bf6ed65a2")},{$pull:{hobbies:"coding"}})
//UPDATE MANY users
db.users.updateMany({name: {$exists:true}},{$unset:{address:""}}) -finds objects where name value exists and removes the address property
//REPLACE
db.users.replaceOne({_id: ObjectId("633315153463fb5bf6ed65a2")},{name:"T"}) - replaces entire object with the given param
///////////////////////////////////////////////////////////////////////////////////
DELETE
//DELETE ONE
db.users.deleteOne({_id: ObjectId("633315153463fb5bf6ed65a2")})
//DELETE MANY
db.users.deleteMany({balance:{$exists:false}}) - deletes all objects without balance property
//DELETE database
db.dropDatabase()