forked from rcos/CSCI2963-01-Spring2017
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-mongodb.js
198 lines (194 loc) · 5.42 KB
/
class-mongodb.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
ngoDB shell version: 3.0.6
connecting to: test
Server has startup warnings:
2016-04-05T12:58:38.821-0400 I CONTROL [initandlisten]
2016-04-05T12:58:38.821-0400 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
>
>
>
>
>
>
> show dbs
local 0.078GB
test 0.078GB
> 0.1 + 0.1 + 0.1 == 0.3
false
>
>
>
>
>
>
>
>
>
> use chairs
switched to db chairs
> db
chairs
>
>
> show collections
>
>
> db.inventory.insert({numLegs: 2})
WriteResult({ "nInserted" : 1 })
> show collections
inventory
system.indexes
> db.inventory
chairs.inventory
> db.inventory.find()
{ "_id" : ObjectId("5703ef7171004779c9d67782"), "numLegs" : 2 }
> db.inventory.insert({numLegs: 3})
WriteResult({ "nInserted" : 1 })
>
> db.inventory.find()
{ "_id" : ObjectId("5703ef7171004779c9d67782"), "numLegs" : 2 }
{ "_id" : ObjectId("5703ef9571004779c9d67783"), "numLegs" : 3 }
>
>
> db.inventory.find().pretty()
{ "_id" : ObjectId("5703ef7171004779c9d67782"), "numLegs" : 2 }
{ "_id" : ObjectId("5703ef9571004779c9d67783"), "numLegs" : 3 }
> db.inventory.insert({numLegs: 3})
WriteResult({ "nInserted" : 1 })
> db.inventory.insert({numLegs: 4})
WriteResult({ "nInserted" : 1 })
>
>
>
> db.inventory.find().pretty()
{ "_id" : ObjectId("5703ef7171004779c9d67782"), "numLegs" : 2 }
{ "_id" : ObjectId("5703ef9571004779c9d67783"), "numLegs" : 3 }
{ "_id" : ObjectId("5703efaf71004779c9d67784"), "numLegs" : 3 }
{ "_id" : ObjectId("5703efb071004779c9d67785"), "numLegs" : 4 }
>
>
>
>
> db.inventory.find({numLegs: 3})
{ "_id" : ObjectId("5703ef9571004779c9d67783"), "numLegs" : 3 }
{ "_id" : ObjectId("5703efaf71004779c9d67784"), "numLegs" : 3 }
> db.inventory.find({_id: ObjectId("5703efb071004779c9d67785")})
{ "_id" : ObjectId("5703efb071004779c9d67785"), "numLegs" : 4 }
>
>
>
>
> db.inventory.update({numLegs: 2}, {numLegs: 3})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
>
> db.inventory.fidn()
2016-04-05T13:04:32.307-0400 E QUERY TypeError: Property 'fidn' of object chairs.inventory is not a function
at (shell):1:14
> db.inventory.find()
{ "_id" : ObjectId("5703ef7171004779c9d67782"), "numLegs" : 3 }
{ "_id" : ObjectId("5703ef9571004779c9d67783"), "numLegs" : 3 }
{ "_id" : ObjectId("5703efaf71004779c9d67784"), "numLegs" : 3 }
{ "_id" : ObjectId("5703efb071004779c9d67785"), "numLegs" : 4 }
>
>
>
>
>
>
> db.inventory.update({numLegs: 3}, {numLegs: 2}, {multi: 1})
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 9,
"errmsg" : "multi update only works with $ operators"
}
})
> db.inventory.update({numLegs: 3}, {numLegs: 2}, {multi: true})
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 9,
"errmsg" : "multi update only works with $ operators"
}
})
>
>
> db.inventory.update({numLegs: 3}, {$set: {numLegs: 2}}, {multi: true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
>
>
> db.inventory.find({ "_id" : ObjectId("5703ef7171004779c9d67782"), "numLegs" : 2 })
{ "_id" : ObjectId("5703ef9571004779c9d67783"), "numLegs" : 2 }
{ "_id" : ObjectId("5703efaf71004779c9d67784"), "numLegs" : 2 }
{ "_id" : ObjectId("5703efb071004779c9d67785"), "numLegs" : 4 }
>
>
>
> db.inventory.update({numLegs: 2}, {$set: {name: "Bob"}}, {multi: true})
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
>
>
> db.inventory.find(){ "_id" : ObjectId("5703efb071004779c9d67785"), "numLegs" : 4 }
{ "_id" : ObjectId("5703ef7171004779c9d67782"), "numLegs" : 2, "name" : "Bob" }
{ "_id" : ObjectId("5703ef9571004779c9d67783"), "numLegs" : 2, "name" : "Bob" }
{ "_id" : ObjectId("5703efaf71004779c9d67784"), "numLegs" : 2, "name" : "Bob" }
>
>
> db.inventory.find({name: "Bob"})
{ "_id" : ObjectId("5703ef7171004779c9d67782"), "numLegs" : 2, "name" : "Bob" }
{ "_id" : ObjectId("5703ef9571004779c9d67783"), "numLegs" : 2, "name" : "Bob" }
{ "_id" : ObjectId("5703efaf71004779c9d67784"), "numLegs" : 2, "name" : "Bob" }
>
>
> db.inventory.find({name: ""})
> db.inventory.find({name: "Bill"})
>
>
>
> db.inventory.find({$exists: {name: 1})
...
... ^C
> db.inventory.find({$exists: {name: 1}})
Error: error: {
"$err" : "Can't canonicalize query: BadValue unknown top level operator: $exists",
"code" : 17287
}
>
>
> db.inventory.find({name: {$exists: 1}})
{ "_id" : ObjectId("5703ef7171004779c9d67782"), "numLegs" : 2, "name" : "Bob" }
{ "_id" : ObjectId("5703ef9571004779c9d67783"), "numLegs" : 2, "name" : "Bob" }
{ "_id" : ObjectId("5703efaf71004779c9d67784"), "numLegs" : 2, "name" : "Bob" }
>
>
> db.inventory.find({name: {$exists: 0}})
{ "_id" : ObjectId("5703efb071004779c9d67785"), "numLegs" : 4 }
>
>
>
> db.inventory.update({name: {$exists: 0}}, {$set: {name: "Bill"})
...
... ^C
> db.inventory.update({name: {$exists: 0}}, {$set: {name:"Bill
2016-04-05T13:09:09.505-0400 E QUERY SyntaxError: Unexpected token ILLEGAL
> db.inventory.update({name: {$exists: 0}}, {$set: {name:"Bill"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>
> db.inventory.find()
{ "_id" : ObjectId("5703ef7171004779c9d67782"), "numLegs" : 2, "name" : "Bob" }
{ "_id" : ObjectId("5703ef9571004779c9d67783"), "numLegs" : 2, "name" : "Bob" }
{ "_id" : ObjectId("5703efaf71004779c9d67784"), "numLegs" : 2, "name" : "Bob" }
{ "_id" : ObjectId("5703efb071004779c9d67785"), "numLegs" : 4, "name" : "Bill" }
>
>
>
>
> ^C
bye
~ %