diff --git a/Doc/Sda1/mongodb.xml b/Doc/Sda1/mongodb.xml index d3499b74770c68a9ad11c012839c027a94f4589b..0092ffc5236e035bd209bad93d1bd377cb528b86 100644 --- a/Doc/Sda1/mongodb.xml +++ b/Doc/Sda1/mongodb.xml @@ -36,28 +36,233 @@ like:</para> <programlisting language="json">{ - "lecture" : { - "title" : "Software development 2", - "prerequisites" : [113105, 113102] - }, + "title" : "Software development 1", "studyCourse" : "MIB", "programme" : "Bachelor", "recommendedSemesters" : { - "semester" : [4, 6, 7], - " comment" : "Advanced topics" + "semester" : [1, 2] + }, + "lecture_id" : 113105 +} + +{ + "title" : "Software development 2", + "prerequisites" : [113105, 113102], + "studyCourse" : "MIB", + "programme" : "Bachelor", + "recommendedSemesters" : { + "semester" : [2, 3, 4] }, "lecture_id" : 113213 -}</programlisting> +} - <para>Modify attribute values to create at least three - different lectures.</para> +{ + "title" : "Object Recognition in Image and Video Data", + "studyCourse" : "CSM", + "programme" : "Master", + "recommendedSemesters" : { + "semester" : [1, 2, 3] + }, + "lecture_id" : 143108 + }</programlisting> + </listitem> + + <listitem xml:id="sda1ItemMongoDefineIndex"> + <para>Define a <link + xlink:href="https://docs.mongodb.com/manual/core/index-unique">unique + index</link> on the <property>lecture_id</property> property + and check whether the MongoDB Server rejects an attempt + inserting a duplicate key value.</para> </listitem> <listitem> - <para/> + <para>List all index definitions.</para> + </listitem> + + <listitem> + <para>Taking a glimpse at <link + xlink:href="https://docs.mongodb.com/getting-started/shell/query">Find + or Query Data with the mongo Shell</link> and craft the + following queries:</para> + + <orderedlist> + <listitem> + <para>All lectures.</para> + </listitem> + + <listitem> + <para>The (unique) lecture having + <property>lecture_id</property>=113213.</para> + </listitem> + + <listitem> + <para>All lectures belonging to a <quote>Master</quote> + programme.</para> + </listitem> + + <listitem> + <para>All lectures being recommended for semester + 4.</para> + </listitem> + + <listitem> + <para>All lectures</para> + </listitem> + </orderedlist> + </listitem> + + <listitem> + <para>Drop your index definition from step <xref + linkend="sda1ItemMongoDefineIndex"/>.</para> </listitem> </orderedlist> </question> + + <answer> + <orderedlist> + <listitem> + <para>Database server acknowledgement <coref + linkend="sda1ProgMongoInsertAck"/> upon insertion of three + lectures <coref linkend="sda1ProgMongoInsertS1"/>, <coref + linkend="sda1ProgMongoInsertS2"/> and <coref + linkend="sda1ProgMongoInsertS1"/> :</para> + + <programlisting language="json">db.university.insertMany( +[ + { + "title" : "Software development 1", + "studyCourse" : "MIB", + "programme" : "Bachelor", + "recommendedSemesters" : { + "semester" : [1, 2] + }, + "lecture_id" : 113105 + }, {... + } +]); + +{ + "acknowledged" : true,<co xml:id="sda1ProgMongoInsertAck"/> + "insertedIds" : [ + ObjectId("586942cb27e04cf209347687"), <co xml:id="sda1ProgMongoInsertS1"/> + ObjectId("586942cb27e04cf209347688"), <co xml:id="sda1ProgMongoInsertS2"/> + ObjectId("586942cb27e04cf209347689") <co xml:id="sda1ProgMongoInsertS3"/> + ] +}</programlisting> + </listitem> + + <listitem> + <para>Defining a <link + xlink:href="https://docs.mongodb.com/manual/core/index-unique">unique + index</link> on the <property>lecture_id</property> + property:</para> + + <programlisting language="json">> db.university.createIndex( { "lecture_id": 1 }, { unique: true } ) +{ + "createdCollectionAutomatically" : false, + "numIndexesBefore" : 1, + "numIndexesAfter" : 2, + "ok" : 1 +}</programlisting> + + <para>Now a duplicate key value insert attempt gets flagged + as an error:</para> + + <programlisting language="json">db.university.insert({ + "title" : "Object Recognition in Image and Video Data", + ... + "lecture_id" : 143108 }) +WriteResult({ + "nInserted" : 0, + "writeError" : { + "code" : 11000, + "errmsg" : "E11000 duplicate key error collection: test.university index: + lecture_id_1 dup key: { : 143108.0 }" + } +})</programlisting> + </listitem> + + <listitem> + <para>Obtaining a list of all index definitions:</para> + + <programlisting language="json">> db.university.getIndexes() +[ + { <co linkends="sda1CalloutMongoListIndexes-1" + xml:id="sda1CalloutMongoListIndexes-1-co"/> + "v" : 2, + "key" : { + "_id" : 1 + }, + "name" : "_id_", + "ns" : "test.university" + }, + { <co linkends="sda1CalloutMongoListIndexes-2" + xml:id="sda1CalloutMongoListIndexes-2-co"/> + "v" : 2, + "unique" : true, + "key" : { + "lecture_id" : 1 + }, + "name" : "lecture_id_1", + "ns" : "test.university" + } +] +></programlisting> + + <calloutlist> + <callout arearefs="sda1CalloutMongoListIndexes-1-co" + xml:id="sda1CalloutMongoListIndexes-1"> + <para>First index being automatically generated by + MogoDB.</para> + </callout> + + <callout arearefs="sda1CalloutMongoListIndexes-2-co" + xml:id="sda1CalloutMongoListIndexes-2"> + <para>Custom key definition from previous step.</para> + </callout> + </calloutlist> + </listitem> + + <listitem> + <para>Taking a glimpse at <link + xlink:href="https://docs.mongodb.com/getting-started/shell/query">Find + or Query Data with the mongo Shell</link> and craft the + following queries:</para> + + <orderedlist> + <listitem> + <para>All lectures.</para> + </listitem> + + <listitem> + <para>The (unique) lecture having + <property>lecture_id</property>=113213.</para> + </listitem> + + <listitem> + <para>All lectures belonging to a <quote>Master</quote> + programme.</para> + </listitem> + + <listitem> + <para>All lectures being recommended for semester + 4.</para> + </listitem> + + <listitem> + <para>All lectures</para> + </listitem> + </orderedlist> + </listitem> + + <listitem> + <para>Dropping a custom index:</para> + + <programlisting language="json">> db.university.dropIndex( { "lecture_id": 1 } ) +{ "nIndexesWas" : 2, "ok" : 1 }</programlisting> + </listitem> + </orderedlist> + </answer> </qandaentry> </qandadiv> </qandaset>