-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcourse.xml
3259 lines (3135 loc) · 125 KB
/
course.xml
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<course url_name='2012_Fall' org='10gen' course='M102' display_name='MongoDB for DBAs' start='2012-10-22T00:00' end='2012-12-25T00:00' enrollment_start='2012-10-01T00:00' enrollment_end='2012-12-02T00:00'>
<chapter start='2012-10-22T00:00' display_name='week1'>
<sequential graded='false' display_name='Course Overview' chapter_name='week1'>
<vertical>
<video display_name='Lecture' youtube='1.0:0euni8AdT1c'/>
</vertical>
</sequential>
<sequential graded='false' display_name='Concepts and Philosophy' chapter_name='week1'>
<vertical>
<video display_name='Lecture' youtube='1.0:QUuYrMSPhL4'/>
</vertical>
</sequential>
<sequential graded='false' display_name='Installing on Unix' chapter_name='week1'>
<vertical>
<video display_name='Lecture' youtube='1.0:6VFukRETCTg'/>
</vertical>
</sequential>
<sequential graded='false' display_name='Installing MongoDB on Windows' chapter_name='week1'>
<vertical>
<video display_name='Lecture' youtube='1.0:hX5louVryOQ'/>
</vertical>
</sequential>
<sequential graded='false' display_name='JSON Types' chapter_name='week1' format='Quiz'>
<vertical>
<video display_name='Lecture' youtube='1.0:gKOcqNHmc4Q'/>
</vertical>
<problem showanswer='attempted' url_name='JSON_Types_problem' rerandomize='never' display_name='Quiz: JSON Types'>
How many data types are there in JSON?<multiplechoiceresponse direction='vertical'>
<choicegroup type='MultipleChoice'>
<choice location='bottom' correct='false'><text>1 (just strings)</text></choice>
<choice location='bottom' correct='false'><text>2</text></choice>
<choice location='bottom' correct='false'><text>4</text></choice>
<choice location='bottom' correct='true'><text>6</text></choice>
</choicegroup>
</multiplechoiceresponse>
</problem>
<vertical start='Oct 30 23:00 EST 2012'>
<video display_name='Answer' youtube='1.0:e2lfdSDENjY'/>
</vertical>
</sequential>
<sequential graded='false' display_name='JSON Syntax' chapter_name='week1' format='Quiz'>
<vertical>
<video display_name='Lecture' youtube='1.0:a3jWXcmjrWM'/>
</vertical>
<problem showanswer='attempted' url_name='JSON_Syntax_problem' rerandomize='never' display_name='Quiz: JSON Syntax'>
<p>What is the corresponding JSON for the following XML document?</p>
<pre>
<person>
<name>John</name>
<age>25</age>
<address>
<city>New York</city>
<postalCode>10021</postalCode>
</address>
<phones>
<phone type="home">212-555-1234</phone>
<phone type="mobile">646-555-1234</phone>
</phones>
</person>
</pre><customresponse>
<textbox rows='4' cols='50' correct_answer='{ "name" : "John", "age" : 25, "address" : { "city" : "New York", "postalCode" : "10021" }, "phones" : [ {"phone":"212-555-1234", "type" : "home"}, {"phone":"646-555-1234", "type" : "mobile"} ] }'> </textbox>
<answer type='loncapa/python'><![CDATA[
import demjson
correct = ['correct']
messages = ['']
try:
r = str(submission[0])
except ValueError:
correct[0] = 'incorrect'
r = '0'
quiz_answer = '''{
"name" : "John",
"age" : 25,
"address" : {
"city" : "New York",
"postalCode" : "10021"
},
"phones" : [
{"phone":"212-555-1234", "type" : "home"},
{"phone":"646-555-1234", "type" : "mobile"}
]
}'''
quiz_answer_json = {}
try:
quiz_answer_json = demjson.decode(quiz_answer)
except demjson.JSONDecodeError as err:
messages[0] = 'Quiz Answer failed to parse' + str(err)
response_json = {}
try:
response_json = demjson.decode(r)
except demjson.JSONDecodeError as err:
messages[0] = 'Response failed to parse' + str(err)
if not(response_json == quiz_answer_json):
correct[0] = 'incorrect'
]]></answer>
</customresponse>
</problem>
<vertical start='Oct 30 23:00 EST 2012'>
<video display_name='Answer' youtube='1.0:RoUvl8c_3Sc'/>
</vertical>
</sequential>
<sequential graded='false' display_name='JSON Syntax 2' chapter_name='week1' format='Quiz'>
<vertical>
<video display_name='Lecture' youtube='1.0:dxd1BnBqQk8'/>
</vertical>
<problem showanswer='attempted' url_name='JSON_Syntax_2_problem' rerandomize='never' display_name='Quiz: JSON Syntax 2'>
For the following XML, Is the corresponding JSON example legal json?
<pre>
<things>
<hat>one</hat>
<coat>z</coat>
<hat>two</hat>
</things>
</pre>
<pre>
{
"hat" : "one",
"coat" : "z",
"hat" : "two"
}
</pre>
<multiplechoiceresponse direction='vertical'>
<choicegroup type='MultipleChoice'>
<choice location='bottom' correct='false'><text>yes</text></choice>
<choice location='bottom' correct='false'><text>no</text></choice>
<choice location='bottom' correct='true'><text>maybe</text></choice>
</choicegroup>
</multiplechoiceresponse>
</problem>
<vertical start='Oct 30 23:00 EST 2012'>
<video display_name='Answer' youtube='1.0:eypM73cF0dI'/>
</vertical>
</sequential>
<sequential graded='false' display_name='Introduction to BSON' chapter_name='week1'>
<vertical>
<video display_name='Lecture' youtube='1.0:CEH0kNxVyMw'/>
</vertical>
</sequential>
<sequential graded='false' display_name='What is the Mongo Shell' chapter_name='week1'>
<vertical>
<video display_name='Lecture' youtube='1.0:W4o8HJyL7_8'/>
</vertical>
</sequential>
<sequential graded='false' display_name='What is JavaScript' chapter_name='week1'>
<vertical>
<video display_name='Lecture' youtube='1.0:n1Hld15pvZY'/>
</vertical>
</sequential>
<sequential graded='false' display_name='What is JavaScript 2' chapter_name='week1'>
<vertical>
<video display_name='Lecture' youtube='1.0:n79mmmQxoNE'/>
</vertical>
</sequential>
<sequential graded='false' display_name='mongoimport' chapter_name='week1' format='Quiz'>
<vertical>
<video display_name='Lecture' youtube='1.0:ZWnHhKiApVU'/>
</vertical>
<problem showanswer='attempted' url_name='mongoimport_problem' rerandomize='never' display_name='Quiz: mongoimport'>
The mongoimport utility can import what types of data?<choiceresponse direction='vertical'>
<checkboxgroup type='MultipleChoice'>
<choice correct='true'>
<startouttext/>CSV (comma separated values)<endouttext/>
</choice>
<choice correct='true'>
<startouttext/>TSV (tab separated values)<endouttext/>
</choice>
<choice correct='false'>
<startouttext/>XML<endouttext/>
</choice>
<choice correct='true'>
<startouttext/>JSON<endouttext/>
</choice>
<choice correct='false'>
<startouttext/>BSON<endouttext/>
</choice>
<choice correct='false'>
<startouttext/>plain text<endouttext/>
</choice>
</checkboxgroup>
</choiceresponse>
</problem>
<vertical start='Oct 30 23:00 EST 2012'>
<video display_name='Answer' youtube='1.0:8_6S7E9r9DQ'/>
</vertical>
</sequential>
<sequential graded='false' display_name='Introduction to the MongoDB Shell' chapter_name='week1'>
<vertical>
<video display_name='Lecture' youtube='1.0:P1yLtecuLM8'/>
</vertical>
</sequential>
<sequential graded='false' display_name='Shell: Queries' chapter_name='week1' format='Quiz'>
<vertical>
<video display_name='Lecture' youtube='1.0:Mjcd5PAdG-s'/>
</vertical>
<problem showanswer='attempted' url_name='Shell_Queries_problem' rerandomize='never' display_name='Quiz: Shell: Queries'>
We have sample documents in our products collection such as:
<pre>
{
name: "AC1 Case Green",
color: "green",
price: 12.00,
for: "ac1",
type: ["accessory", "case"],
available: true
}
</pre>
How would we query in the shell for all products that are cases for an ac9 phone? That is, where <i>type</i> contains the value "case" and <i>for</i> equals "ac9"?
<p><i>Please use field names in the order they appear in the question and use double quotes around values.</i></p><customresponse>
<textbox rows='4' cols='50' correct_answer='db.products.find({type: "case", for: "ac9"})'> </textbox>
<answer type='loncapa/python'><![CDATA[
try:
response = str(submission[0])
except ValueError:
correct[0] = 'incorrect'
response = '0'
answer = '''db.products.find({type: "case", for: "ac9"})'''
# Filter out newlines, carriage returns, and semicolons.
import demjson
import re
response = re.sub(r'[\s;]+', '', response)
answer = re.sub(r'[\s;]+', '', answer)
# Create a JSON string representation of the input string.
def json_encode(text):
import demjson
import re
# The global scope is not carried here, so we must internally define functions.
class Match(object):
start = 0
end = 0
def __init__(self, start, end):
self.start = start
self.end = end
# Extract embedded JSON.
def capture(text, start):
pos = start
match = Match(-1, -1)
brace_count = 0
while pos < len(text):
if text[pos] == '{':
# Start checking for a JSON object.
if brace_count == 0:
match.start = pos
brace_count = brace_count + 1
elif text[pos] == '}':
# Check for a complete JSON object.
brace_count = brace_count - 1
if brace_count == 0:
match.end = pos + 1
match.str = text[match.start : match.end]
break
pos = pos + 1
if match.start == -1 or match.end == -1:
return None
return match
# Now perform actual answer-to-json conversion.
text_out = '{'
key_index_out = 0
# Start parsing and splitting the string.
pos = 0
match = capture(text, pos)
while match != None:
# There was some text between matches, capture it.
if pos != match.start:
text_out += '"s' + str(key_index_out) + '" : "' + re.sub('"', '', text[pos:match.start]) + '", '
key_index_out = key_index_out + 1
text_out += '"s' + str(key_index_out) + '" : ' + match.str + ', '
key_index_out = key_index_out + 1
pos = match.end
match = capture(text, pos)
# Get any text after the last match.
text_out += '"s' + str(key_index_out) + '" : "' + re.sub('"', '', text[pos:]) + '", '
text_out += '}'
return str(text_out)
correct = ['incorrect']
messages = ['']
try:
response_json = demjson.decode(json_encode(response))
answer_json = demjson.decode(json_encode(answer))
if response_json == answer_json:
correct[0] = 'correct'
except demjson.JSONDecodeError as err:
messages[0] = "Response failed to parse " + str(err)
]]>
</answer>
</customresponse>
</problem>
<vertical start='Oct 30 23:00 EST 2012'>
<video display_name='Answer' youtube='1.0:fYcPCRCPvDg'/>
</vertical>
</sequential>
<sequential graded='false' display_name='Shell: Sorting' chapter_name='week1'>
<vertical>
<video display_name='Lecture' youtube='1.0:Ezx1lXaY7jE'/>
</vertical>
</sequential>
<sequential graded='false' display_name='Shell: Cursors and Shell Help' chapter_name='week1' format='Quiz'>
<vertical>
<video display_name='Lecture' youtube='1.0:DRs4_hBpPlI'/>
</vertical>
<problem showanswer='attempted' url_name='Shell_Cursors_and_Shell_Help_problem' rerandomize='never' display_name='Quiz: Shell: Cursors and Shell Help'>
What would you type to see the help/man text for mongo cursors in the mongo administrative shell?<multiplechoiceresponse direction='vertical'>
<choicegroup type='MultipleChoice'>
<choice location='bottom' correct='false'><text>help</text></choice>
<choice location='bottom' correct='false'><text>db.help()</text></choice>
<choice location='bottom' correct='false'><text>db.products.help()</text></choice>
<choice location='bottom' correct='false'><text>help misc</text></choice>
<choice location='bottom' correct='true'><text>db.products.find().help()</text></choice>
</choicegroup>
</multiplechoiceresponse>
</problem>
<vertical start='Oct 30 23:00 EST 2012'>
<video display_name='Answer' youtube='1.0:3KwsHVHBM9w'/>
</vertical>
</sequential>
<sequential graded='true' display_name='Homework 1.1' chapter_name='week1' format='Homework' due='Oct 30 23:00 EST 2012'>
<problem showanswer='due' attempts='3' end='2012-12-25T00:00' url_name='Homework_1_1_problem' rerandomize='never' display_name='Homework: Homework 1.1'>
Download and install MongoDB from www.mongodb.org. Then run the database as a single server instance on your PC (that is, run the mongod binary). Then, run the administrative shell. From the shell prompt type
<pre>
db.isMaster().maxBsonObjectSize
</pre>
at the ">" prompt. What do you get as a result?<customresponse>
<textbox rows='4' cols='50' correct_answer="16777216"> </textbox>
<answer type='loncapa/python'><![CDATA[
import re
correct = ['correct']
try:
r = str(submission[0])
except ValueError:
correct[0] = 'incorrect'
r = '0'
response = re.sub(r'\s+', '', r)
quiz_answer = '''16777216'''
quiz_answer = re.sub(r'\s+', '', quiz_answer)
if not(response == quiz_answer):
correct[0] = 'incorrect'
print 'response = ', response
print 'quiz_answer = ', quiz_answer
]]>
</answer>
</customresponse>
</problem>
</sequential>
<sequential graded='true' display_name='Homework 1.2' chapter_name='week1' format='Homework' due='Oct 30 23:00 EST 2012'>
<problem showanswer='due' attempts='3' end='2012-12-25T00:00' url_name='Homework_1_2_problem' rerandomize='never' display_name='Homework: Homework 1.2'>
Download the file <a href="/static/handouts/products.json">products.json</a> from education.10gen.com.
Take a look at its content.
Now, import its contents into MongoDB, into a database called "pcat" and a collection called "products". Use the mongoimport utility to do this.
When done, run this query in the mongo shell:
<pre>
db.products.find({type:"case"}).count()
</pre>
What's the result?<customresponse>
<textbox rows='4' cols='50' correct_answer="3"> </textbox>
<answer type='loncapa/python'><![CDATA[
import re
correct = ['correct']
try:
r = str(submission[0])
except ValueError:
correct[0] = 'incorrect'
r = '0'
response = re.sub(r'\s+', '', r)
quiz_answer = '''3'''
quiz_answer = re.sub(r'\s+', '', quiz_answer)
if not(response == quiz_answer):
correct[0] = 'incorrect'
print 'response = ', response
print 'quiz_answer = ', quiz_answer
]]>
</answer>
</customresponse>
</problem>
</sequential>
<sequential graded='true' display_name='Homework 1.3' chapter_name='week1' format='Homework' due='Oct 30 23:00 EST 2012'>
<problem showanswer='due' attempts='3' end='2012-12-25T00:00' url_name='Homework_1_3_problem' rerandomize='never' display_name='Homework: Homework 1.3'>
At this point you should have pcat.products loaded from the previous step. You can confirm this by running in the shell:
<pre>
> db.products.find()
> // or:
> db.products.count()
> // should print out "11"
</pre>
Now, what query would you run to get all the products where brand equals the string “ACME”?<customresponse>
<textbox rows='4' cols='50' correct_answer='db.products.find({brand:"ACME"})'> </textbox>
<answer type='loncapa/python'><![CDATA[
try:
response = str(submission[0])
except ValueError:
correct[0] = 'incorrect'
response = '0'
answer = '''db.products.find({brand:"ACME"})'''
# Filter out newlines, carriage returns, and semicolons.
import demjson
import re
response = re.sub(r'[\s;]+', '', response)
answer = re.sub(r'[\s;]+', '', answer)
# Create a JSON string representation of the input string.
def json_encode(text):
import demjson
import re
# The global scope is not carried here, so we must internally define functions.
class Match(object):
start = 0
end = 0
def __init__(self, start, end):
self.start = start
self.end = end
# Extract embedded JSON.
def capture(text, start):
pos = start
match = Match(-1, -1)
brace_count = 0
while pos < len(text):
if text[pos] == '{':
# Start checking for a JSON object.
if brace_count == 0:
match.start = pos
brace_count = brace_count + 1
elif text[pos] == '}':
# Check for a complete JSON object.
brace_count = brace_count - 1
if brace_count == 0:
match.end = pos + 1
match.str = text[match.start : match.end]
break
pos = pos + 1
if match.start == -1 or match.end == -1:
return None
return match
# Now perform actual answer-to-json conversion.
text_out = '{'
key_index_out = 0
# Start parsing and splitting the string.
pos = 0
match = capture(text, pos)
while match != None:
# There was some text between matches, capture it.
if pos != match.start:
text_out += '"s' + str(key_index_out) + '" : "' + re.sub('"', '', text[pos:match.start]) + '", '
key_index_out = key_index_out + 1
text_out += '"s' + str(key_index_out) + '" : ' + match.str + ', '
key_index_out = key_index_out + 1
pos = match.end
match = capture(text, pos)
# Get any text after the last match.
text_out += '"s' + str(key_index_out) + '" : "' + re.sub('"', '', text[pos:]) + '", '
text_out += '}'
return str(text_out)
correct = ['incorrect']
messages = ['']
try:
response_json = demjson.decode(json_encode(response))
answer_json = demjson.decode(json_encode(answer))
if response_json == answer_json:
correct[0] = 'correct'
except demjson.JSONDecodeError as err:
messages[0] = "Response failed to parse " + str(err)
]]>
</answer>
</customresponse>
</problem>
</sequential>
<sequential graded='true' display_name='Homework 1.4' chapter_name='week1' format='Homework' due='Oct 30 23:00 EST 2012'>
<problem showanswer='due' attempts='3' end='2012-12-25T00:00' url_name='Homework_1_4_problem' rerandomize='never' display_name='Homework: Homework 1.4'>
How would you print out, in the shell, the name of all the products without extraneous characters or braces, sorted alphabetically, ascending? (Check all that would apply.)<choiceresponse direction='vertical'>
<checkboxgroup type='MultipleChoice'>
<choice correct='false'>
<startouttext/>db.products.find({},{name:1,_id:0}).sort({name:1})<endouttext/>
</choice>
<choice correct='true'>
<startouttext/>var c = db.products.find({},{name:1,_id:0}).sort({name:1});
while( c.hasNext() )
print( c.next().name);
<endouttext/>
</choice>
<choice correct='true'>
<startouttext/>var c = db.products.find({}).sort({name:1});
c.forEach( function(doc){ print(doc.name) } );<endouttext/>
</choice>
<choice correct='false'>
<startouttext/>var c = db.products.find({}).sort({name:-1});
while( c.hasNext() )
print( c.next().name);<endouttext/>
</choice>
</checkboxgroup>
</choiceresponse>
</problem>
</sequential>
</chapter>
<chapter start='2012-10-29T00:00' display_name='week2'>
<sequential graded='false' display_name='Inserting Data' chapter_name='week2' format='Quiz'>
<vertical>
<video display_name='Lecture' youtube='1.0:MWH-_nJgebw'/>
</vertical>
<problem showanswer='attempted' url_name='Inserting_Data_problem' rerandomize='never' display_name='Quiz: Inserting Data'>
In the mongo shell, what would you type to insert the document {x:3,y:4} into the "temperature" collection for the current database?<customresponse>
<textbox rows='4' cols='50' correct_answer="db.temperature.insert({x:3,y:4})"> </textbox>
<answer type='loncapa/python'><![CDATA[
try:
response = str(submission[0])
except ValueError:
correct[0] = 'incorrect'
response = '0'
answer = '''db.temperature.insert({x:3,y:4})'''
# Filter out newlines, carriage returns, and semicolons.
import demjson
import re
response = re.sub(r'[\s;]+', '', response)
answer = re.sub(r'[\s;]+', '', answer)
# Create a JSON string representation of the input string.
def json_encode(text):
import demjson
import re
# The global scope is not carried here, so we must internally define functions.
class Match(object):
start = 0
end = 0
def __init__(self, start, end):
self.start = start
self.end = end
# Extract embedded JSON.
def capture(text, start):
pos = start
match = Match(-1, -1)
brace_count = 0
while pos < len(text):
if text[pos] == '{':
# Start checking for a JSON object.
if brace_count == 0:
match.start = pos
brace_count = brace_count + 1
elif text[pos] == '}':
# Check for a complete JSON object.
brace_count = brace_count - 1
if brace_count == 0:
match.end = pos + 1
match.str = text[match.start : match.end]
break
pos = pos + 1
if match.start == -1 or match.end == -1:
return None
return match
# Now perform actual answer-to-json conversion.
text_out = '{'
key_index_out = 0
# Start parsing and splitting the string.
pos = 0
match = capture(text, pos)
while match != None:
# There was some text between matches, capture it.
if pos != match.start:
text_out += '"s' + str(key_index_out) + '" : "' + re.sub('"', '', text[pos:match.start]) + '", '
key_index_out = key_index_out + 1
text_out += '"s' + str(key_index_out) + '" : ' + match.str + ', '
key_index_out = key_index_out + 1
pos = match.end
match = capture(text, pos)
# Get any text after the last match.
text_out += '"s' + str(key_index_out) + '" : "' + re.sub('"', '', text[pos:]) + '", '
text_out += '}'
return str(text_out)
correct = ['incorrect']
messages = ['']
try:
response_json = demjson.decode(json_encode(response))
answer_json = demjson.decode(json_encode(answer))
if response_json == answer_json:
correct[0] = 'correct'
except demjson.JSONDecodeError as err:
messages[0] = "Response failed to parse " + str(err)
]]>
</answer>
</customresponse>
</problem>
<vertical start='Nov 8 23:00 EST 2012'>
<video display_name='Answer' youtube='1.0:Yl6L0jytr4o'/>
</vertical>
</sequential>
<sequential graded='false' display_name='Updating Documents' chapter_name='week2' format='Quiz'>
<vertical>
<video display_name='Lecture' youtube='1.0:0tGJGxqvO0c'/>
</vertical>
<problem showanswer='attempted' url_name='Updating_Documents_problem' rerandomize='never' display_name='Quiz: Updating Documents'>
Suppose we have a collection "cars" with one document preexisting:
<pre>
{ "_id" : 100, "name" : "GTO",
"year" : 1969, "color" : "red" }
</pre>
Which of the following statements would set "available" to 1?<choiceresponse direction='vertical'>
<checkboxgroup type='MultipleChoice'>
<choice correct='true'>
<startouttext/>db.cars.update({_id:100},{$set:{available:1}})<endouttext/>
</choice>
<choice correct='true'>
<startouttext/>db.cars.update({_id:100},{$inc:{available:1}})<endouttext/>
</choice>
<choice correct='true'>
<startouttext/>db.cars.update({},{$inc:{available:1,year:1970}})<endouttext/>
</choice>
</checkboxgroup>
</choiceresponse>
</problem>
<vertical start='Nov 8 23:00 EST 2012'>
<video display_name='Answer' youtube='1.0:sRjVWZ6olqI'/>
</vertical>
</sequential>
<sequential graded='false' display_name='MongoDB Documents Quiz' chapter_name='week2' format='Quiz'>
<problem showanswer='attempted' url_name='MongoDB_Documents_Quiz_problem' rerandomize='never' display_name='Quiz: MongoDB Documents Quiz'>
Which of the following are true?<choiceresponse direction='vertical'>
<checkboxgroup type='MultipleChoice'>
<choice correct='true'>
<startouttext/>MongoDB can store large things; for example a 1 terabyte video.<endouttext/>
</choice>
<choice correct='false'>
<startouttext/>On an update of a document in mongodb, the document always physically moves in its position in the data file(s).<endouttext/>
</choice>
<choice correct='false'>
<startouttext/>On average, mongodb documents have about the same number of fields as one would have in a relational database table for the same problem.<endouttext/>
</choice>
</checkboxgroup>
</choiceresponse>
</problem>
</sequential>
<sequential graded='false' display_name='Removing documents' chapter_name='week2' format='Quiz'>
<vertical>
<video display_name='Lecture' youtube='1.0:qj7n4TRVY1M'/>
</vertical>
<problem showanswer='attempted' url_name='Removing_documents_problem' rerandomize='never' display_name='Quiz: Removing documents'>
Suppose we have documents in a 'users' collection of the form:
<pre>
{
_id : ObjectId("50897dbb9b96971d287202a9"),
name : "Jane",
likes : [ "tennis", "golf" ],
registered : false,
addr : {
city : "Lyon",
country : "France"
}
}
</pre>
How would we, in the mongo shell, delete all documents in the collection where city = "Lyon" and registered is false?<customresponse>
<textbox rows='4' cols='50' correct_answer='db.users.remove({"addr.city":"Lyon", registered:false})'> </textbox>
<answer type='loncapa/python'><![CDATA[
try:
response = str(submission[0])
except ValueError:
correct[0] = 'incorrect'
response = '0'
answer = '''db.users.remove({"addr.city":"Lyon",
registered:false})'''
# Filter out newlines, carriage returns, and semicolons.
import demjson
import re
response = re.sub(r'[\s;]+', '', response)
answer = re.sub(r'[\s;]+', '', answer)
# Create a JSON string representation of the input string.
def json_encode(text):
import demjson
import re
# The global scope is not carried here, so we must internally define functions.
class Match(object):
start = 0
end = 0
def __init__(self, start, end):
self.start = start
self.end = end
# Extract embedded JSON.
def capture(text, start):
pos = start
match = Match(-1, -1)
brace_count = 0
while pos < len(text):
if text[pos] == '{':
# Start checking for a JSON object.
if brace_count == 0:
match.start = pos
brace_count = brace_count + 1
elif text[pos] == '}':
# Check for a complete JSON object.
brace_count = brace_count - 1
if brace_count == 0:
match.end = pos + 1
match.str = text[match.start : match.end]
break
pos = pos + 1
if match.start == -1 or match.end == -1:
return None
return match
# Now perform actual answer-to-json conversion.
text_out = '{'
key_index_out = 0
# Start parsing and splitting the string.
pos = 0
match = capture(text, pos)
while match != None:
# There was some text between matches, capture it.
if pos != match.start:
text_out += '"s' + str(key_index_out) + '" : "' + re.sub('"', '', text[pos:match.start]) + '", '
key_index_out = key_index_out + 1
text_out += '"s' + str(key_index_out) + '" : ' + match.str + ', '
key_index_out = key_index_out + 1
pos = match.end
match = capture(text, pos)
# Get any text after the last match.
text_out += '"s' + str(key_index_out) + '" : "' + re.sub('"', '', text[pos:]) + '", '
text_out += '}'
return str(text_out)
correct = ['incorrect']
messages = ['']
try:
response_json = demjson.decode(json_encode(response))
answer_json = demjson.decode(json_encode(answer))
if response_json == answer_json:
correct[0] = 'correct'
except demjson.JSONDecodeError as err:
messages[0] = "Response failed to parse " + str(err)
]]>
</answer>
</customresponse>
</problem>
<vertical start='Nov 8 23:00 EST 2012'>
<video display_name='Answer' youtube='1.0:WOzekUwo1vE'/>
</vertical>
</sequential>
<sequential graded='false' display_name='Updating Part 2' chapter_name='week2' format='Quiz'>
<vertical>
<video display_name='Lecture' youtube='1.0:pgu3nta2iLM'/>
</vertical>
<problem showanswer='attempted' url_name='Updating_Part_2_problem' rerandomize='never' display_name='Quiz: Updating Part 2'>
Suppose we have documents in a 'users' collection of the form:
<pre>
{
_id : "Jane",
likes : [ "tennis", "golf" ],
registered : false,
addr : {
city : "Lyon",
country : "France"
}
}
</pre>
How would we, in the mongo shell, add that this user likes "football"?<customresponse>
<textbox rows='4' cols='50' correct_answer='db.users.update({_id:"Jane"},{$addToSet:{likes:"football"}}, true)'> </textbox>
<answer type='loncapa/python'><![CDATA[
try:
response = str(submission[0])
except ValueError:
correct[0] = 'incorrect'
response = '0'
answer = '''db.users.update({_id:"Jane"},{$addToSet:{likes:"football"}}, true)'''
# Filter out newlines, carriage returns, and semicolons.
import demjson
import re
response = re.sub(r'[\s;]+', '', response)
answer = re.sub(r'[\s;]+', '', answer)
# Create a JSON string representation of the input string.
def json_encode(text):
import demjson
import re
# The global scope is not carried here, so we must internally define functions.
class Match(object):
start = 0
end = 0
def __init__(self, start, end):
self.start = start
self.end = end
# Extract embedded JSON.
def capture(text, start):
pos = start
match = Match(-1, -1)
brace_count = 0
while pos < len(text):
if text[pos] == '{':
# Start checking for a JSON object.
if brace_count == 0:
match.start = pos
brace_count = brace_count + 1
elif text[pos] == '}':
# Check for a complete JSON object.
brace_count = brace_count - 1
if brace_count == 0:
match.end = pos + 1
match.str = text[match.start : match.end]
break
pos = pos + 1
if match.start == -1 or match.end == -1:
return None
return match
# Now perform actual answer-to-json conversion.
text_out = '{'
key_index_out = 0
# Start parsing and splitting the string.
pos = 0
match = capture(text, pos)
while match != None:
# There was some text between matches, capture it.
if pos != match.start:
text_out += '"s' + str(key_index_out) + '" : "' + re.sub('"', '', text[pos:match.start]) + '", '
key_index_out = key_index_out + 1
text_out += '"s' + str(key_index_out) + '" : ' + match.str + ', '
key_index_out = key_index_out + 1
pos = match.end
match = capture(text, pos)
# Get any text after the last match.
text_out += '"s' + str(key_index_out) + '" : "' + re.sub('"', '', text[pos:]) + '", '
text_out += '}'
return str(text_out)
correct = ['incorrect']
messages = ['']
try:
response_json = demjson.decode(json_encode(response))
answer_json = demjson.decode(json_encode(answer))
if response_json == answer_json:
correct[0] = 'correct'
except demjson.JSONDecodeError as err:
messages[0] = "Response failed to parse " + str(err)
]]>
</answer>
</customresponse>
</problem>
<vertical start='Nov 8 23:00 EST 2012'>
<video display_name='Answer' youtube='1.0:zKDmE590nlA'/>
</vertical>
</sequential>
<sequential graded='false' display_name='MongoDB Commands' chapter_name='week2'>
<vertical>
<video display_name='Lecture' youtube='1.0:nAfLu7vKiRI'/>
</vertical>
</sequential>
<sequential graded='false' display_name='MongoDB Commands 2' chapter_name='week2' format='Quiz'>
<vertical>
<video display_name='Lecture' youtube='1.0:0tnBD_hLUso'/>
</vertical>
<problem showanswer='attempted' url_name='MongoDB_Commands_2_problem' rerandomize='never' display_name='Quiz: MongoDB Commands 2'>
Which of these statements is true?
<choiceresponse direction='vertical'>
<checkboxgroup type='MultipleChoice'>
<choice correct='false'>
<startouttext/>MongoDB commands are for administrator use only.<endouttext/>
</choice>
<choice correct='true'>
<startouttext/>In MongoDB collections are usually created implicitly.<endouttext/>
</choice>
<choice correct='false'>
<startouttext/>In MongoDB indexes are usually created implicitly.<endouttext/>
</choice>
</checkboxgroup>
</choiceresponse>
</problem>
<vertical start='Nov 8 23:00 EST 2012'>
<video display_name='Answer' youtube='1.0:uktnp4cIjZk'/>
</vertical>
</sequential>
<sequential graded='true' display_name='Homework 2.1' chapter_name='week2' format='Homework' due='Nov 8 23:00 EST 2012'>
<problem showanswer='due' attempts='3' end='2012-12-25T00:00' url_name='Homework_2_1_problem' rerandomize='never' display_name='Homework: Homework 2.1'>
We will use the pcat.products collection from week 1. So start with that; if not already set up, import it:
<pre>
mongoimport --db pcat -c products < products.json
</pre>
You can find <a href="/static/handouts/products.json">products.json</a> on the education.10gen.com site.
In the shell, if you type:
<pre>
db.products.count()
</pre>
should return 11.
<p>
Next, download <a href="/static/handouts/homework2.js">homework2.js</a> from the education.10gen.com site. Run the shell with this script:</p>
<pre>
mongo --shell pcat homework2.js
</pre>
First, make a mini-backup of the collection before we start modifying it. In the shell:
<pre>
b = db.products_bak; db.products.find().forEach( function(o){ b.insert(o) } )
// check it worked:
b.count()
// should print 11
</pre>
If you have any issues you can restore from "products_bak"; or, you can re-import with mongoimport. (You would perhaps need in that situation to empty the collection first or drop it; see the --drop option on mongoimport --help.)
At the shell ">" prompt type:
<pre>
homework.a()
</pre>
What is the output? (The above will check that products_bak is populated.)<customresponse>
<textbox rows='4' cols='50' correct_answer="3.05"> </textbox>
<answer type='loncapa/python'><![CDATA[
import re
correct = ['correct']
try:
r = str(submission[0])
except ValueError:
correct[0] = 'incorrect'
r = '0'
response = re.sub(r'\s+', '', r)
quiz_answer = '''3.05'''
quiz_answer = re.sub(r'\s+', '', quiz_answer)
if not(response == quiz_answer):
correct[0] = 'incorrect'
print 'response = ', response
print 'quiz_answer = ', quiz_answer
]]>
</answer>
</customresponse>
</problem>
<vertical start='Nov 8 23:00 EST 2012' end='2012-12-25T00:00'>
<video display_name='Answer' youtube='1.0:r8szX-Q-3Hg'/>
</vertical>
</sequential>
<sequential graded='true' display_name='Homework 2.2' chapter_name='week2' format='Homework' due='Nov 8 23:00 EST 2012'>
<problem showanswer='due' attempts='3' end='2012-12-25T00:00' url_name='Homework_2_2_problem' rerandomize='never' display_name='Homework: Homework 2.2'>
Add a new product to the products collection of this form:
<pre>
{
"_id" : "ac9",
"name" : "AC9 Phone",
"brand" : "ACME",
"type" : "phone",
"price" : 333,
"warranty_years" : 0.25,
"available" : true
}
</pre>
Note: in general because of the automatic line continuation in the shell, you can cut/paste in the above and shouldn't have to type it all out. Just enclose it in the proper statement(s) to get it added.
Next, load into a shell variable the object corresponding to
<pre>
_id : ObjectId("507d95d5719dbef170f15c00")
</pre>
<ul>
<li>Then change <code>term_years</code> to 3 for that document. (And save that to the database.)</li>
<li>Then change <code>over_rate</code> for <code>sms</code> in <code>limits</code> to 0.01 from 0. Save that too.</li>
</ul>
At the shell prompt type:
<pre>
homework.b()
</pre>
What is the output?<customresponse>
<textbox rows='4' cols='50' correct_answer="0.050.019031"> </textbox>
<answer type='loncapa/python'><![CDATA[
import re
correct = ['correct']
try:
r = str(submission[0])
except ValueError:
correct[0] = 'incorrect'
r = '0'
response = re.sub(r'\s+', '', r)
quiz_answer = '''0.050.019031'''