Skip to content
Snippets Groups Projects
Commit e26d1a82 authored by Goik Martin's avatar Goik Martin
Browse files

Completing query exercise

parent d639942f
No related branches found
No related tags found
No related merge requests found
......@@ -23,25 +23,21 @@
<qandadiv>
<qandaentry>
<question>
<para>This exercise aims at getting acquainted with basic <xref
linkend="glo_MongoDB"/> create, read, update and delete tasks
based on using the command line interface.</para>
<para>This exercise set aims at getting acquainted with basic
<xref linkend="glo_MongoDB"/> <code>create</code>,
<code>read</code>, <code>update</code> and <code>delete</code>
operations using the command line interface.</para>
<orderedlist>
<listitem>
<para>Follow <uri
xlink:href="https://docs.mongodb.com/getting-started/shell/introduction">https://docs.mongodb.com/getting-started/shell/introduction</uri>
(skipping the already completed installation section)
populating your database with multiple lecture entries
like:</para>
<para>Follow <uri
xlink:href="https://docs.mongodb.com/getting-started/shell/introduction">https://docs.mongodb.com/getting-started/shell/introduction</uri>
(skipping the already completed installation section) populating
your database with multiple lecture entries like:</para>
<programlisting language="json">{
<programlisting language="json">{
"title" : "Software development 1",
"studyCourse" : "MIB",
"programme" : "Bachelor",
"recommendedSemesters" : {
"semester" : [1, 2]
},
"recommendedSemesters" : [1, 2],
"lecture_id" : 113105
}
......@@ -50,9 +46,7 @@
"prerequisites" : [113105, 113102],
"studyCourse" : "MIB",
"programme" : "Bachelor",
"recommendedSemesters" : {
"semester" : [2, 3, 4]
},
"recommendedSemesters" : [2, 3, 4],
"lecture_id" : 113213
}
......@@ -60,85 +54,30 @@
"title" : "Object Recognition in Image and Video Data",
"studyCourse" : "CSM",
"programme" : "Master",
"recommendedSemesters" : {
"semester" : [1, 2, 3]
},
"recommendedSemesters" : [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>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>
<para>Database server acknowledgement <coref
linkend="sda1ProgMongoInsertAck"/> upon successful insertion
<coref linkend="sda1ProgMongoInsertS1"/>, <coref
linkend="sda1ProgMongoInsertS2"/> and <coref
linkend="sda1ProgMongoInsertS1"/> of three lectures :</para>
<programlisting language="json">db.university.insertMany(
<programlisting language="json">db.university.insert(
[
{
"title" : "Software development 1",
"studyCourse" : "MIB",
"programme" : "Bachelor",
"recommendedSemesters" : {
"semester" : [1, 2]
},
"recommendedSemesters" : [1, 2],
"lecture_id" : 113105
}, {...
}
}, {
"title" : "Software development 2",
...
}, ...
]);
{
......@@ -149,15 +88,22 @@
ObjectId("586942cb27e04cf209347689") <co xml:id="sda1ProgMongoInsertS3"/>
]
}</programlisting>
</listitem>
</answer>
</qandaentry>
</qandadiv>
<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>
<qandadiv xml:id="sda1ItemMongoDefineIndex">
<qandaentry>
<question>
<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 a duplicate key
insertion attempt.</para>
</question>
<programlisting language="json">&gt; db.university.createIndex( { "lecture_id": 1 }, { unique: true } )
<answer>
<programlisting language="json">&gt; db.university.createIndex( { "lecture_id": 1 }, { unique: true } )
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
......@@ -165,30 +111,51 @@
"ok" : 1
}</programlisting>
<para>Now a duplicate key value insert attempt gets flagged
as an error:</para>
<para>Now a duplicate key value insert attempt gets flagged as
an error:</para>
<programlisting language="json">db.university.insert({
<programlisting language="json">db.university.insert({
"title" : "Object Recognition in Image and Video Data",
...
"lecture_id" : 143108 })
WriteResult({
"nInserted" : 0,
"writeError" : {
"<emphasis role="bold">writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: test.university index:
lecture_id_1 dup key: { : 143108.0 }"
lecture_id_1 dup key: { : 143108.0 }"</emphasis>
}
})</programlisting>
</listitem>
</answer>
</qandaentry>
</qandadiv>
<listitem>
<para>Obtaining a list of all index definitions:</para>
<qandadiv>
<qandaentry>
<question>
<para>List all index definitions. Explain the result.</para>
</question>
<programlisting language="json">&gt; db.university.getIndexes()
<answer>
<para>Our collection contains two index definitions:</para>
<calloutlist>
<callout arearefs="sda1CalloutMongoListIndexes-1-co"
xml:id="sda1CalloutMongoListIndexes-1">
<para>First index on MogoDB auto generated attribute.</para>
</callout>
<callout arearefs="sda1CalloutMongoListIndexes-2-co"
xml:id="sda1CalloutMongoListIndexes-2">
<para>Custom unique index definition resulting from previous
step.</para>
</callout>
</calloutlist>
<programlisting language="json">&gt; db.university.getIndexes()
[
{ <co linkends="sda1CalloutMongoListIndexes-1"
xml:id="sda1CalloutMongoListIndexes-1-co"/>
xml:id="sda1CalloutMongoListIndexes-1-co"/>
"v" : 2,
"key" : {
"_id" : 1
......@@ -197,7 +164,7 @@ WriteResult({
"ns" : "test.university"
},
{ <co linkends="sda1CalloutMongoListIndexes-2"
xml:id="sda1CalloutMongoListIndexes-2-co"/>
xml:id="sda1CalloutMongoListIndexes-2-co"/>
"v" : 2,
"unique" : true,
"key" : {
......@@ -206,62 +173,144 @@ WriteResult({
"name" : "lecture_id_1",
"ns" : "test.university"
}
]
&gt;</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>
]</programlisting>
</answer>
</qandaentry>
</qandadiv>
<qandadiv>
<qandaentry>
<question>
<para>Take 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>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>
<para>The (unique) lecture having
<property>lecture_id</property>=113213.</para>
</listitem>
<listitem>
<para>Dropping a custom index:</para>
<para>The <property>title</property> values of all lectures
belonging to a <quote>Bachelor</quote> programme suppressing
the auto generated <property>_id</property> property</para>
</listitem>
<programlisting language="json">&gt; db.university.dropIndex( { "lecture_id": 1 } )
{ "nIndexesWas" : 2, "ok" : 1 }</programlisting>
<listitem>
<para>All lectures being recommended for semester 3.</para>
</listitem>
</orderedlist>
</question>
<answer>
<glosslist>
<glossentry>
<glossterm>All lectures.</glossterm>
<glossdef>
<para>Querying all objects of given collection:</para>
<programlisting language="json">&gt; db.university.find()
{ "_id" : ObjectId("586a230651557f1957460a37"), "title" : "Software development 1", ..., "lecture_id" : 113105 }
{ "_id" : ObjectId("586a230651557f1957460a38"), "title" : "Software development 2", ..., "lecture_id" : 113213 }
{ "_id" : ObjectId("586a230651557f1957460a39"), "title" : "Object Recognition ...", ..., "lecture_id" : 143108 }</programlisting>
</glossdef>
</glossentry>
<glossentry>
<glossterm>The (unique) lecture having
<property>lecture_id</property>=113213.</glossterm>
<glossdef>
<programlisting language="json">db.university.find({lecture_id : 143108})
{
"_id" : ObjectId("586a230651557f1957460a39"),
"title" : "Object Recognition in Image and Video Data",
...
<emphasis role="bold">"lecture_id" : 143108</emphasis>}</programlisting>
</glossdef>
</glossentry>
<glossentry>
<glossterm>The <property>title</property> values of all
lectures belonging to a <quote>Bachelor</quote> programme
suppressing the auto generated _id property</glossterm>
<glossdef>
<programlisting language="json">db.university.find({programme : "Bachelor"}, {title: 1, _id: 0})
{ "title" : "Software development 1" }
{ "title" : "Software development 2" }</programlisting>
</glossdef>
</glossentry>
<glossentry>
<glossterm>Properties <property>title</property> and
<property>recommendedSemesters</property> of all lectures
being recommended for semester 3</glossterm>
<glossdef>
<programlisting language="json">db.university.find({recommendedSemesters: 3}, {title:1, recommendedSemesters:1, _id:0})
{ "title" : "Software development 2", "recommendedSemesters" : [ 2, 3, 4 ] }
{ "title" : "Object Recognition in Image and Video Data", "recommendedSemesters" : [ 1, 2, 3 ] }</programlisting>
</glossdef>
</glossentry>
</glosslist>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>Drop all your collection's data records. Will your
database be completely in its previous state?</para>
</question>
<answer>
<programlisting language="json">db.university.remove({});
WriteResult({ "nRemoved" : 3 })</programlisting>
<para>We identify three deleted records corresponding to our
initial inserts. The index definitions however remain:</para>
<programlisting language="json">&gt; db.university.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.university"
},
{
"v" : 2,
"unique" : true,
"key" : {
"lecture_id" : 1
},
"name" : "lecture_id_1",
"ns" : "test.university"
}
]</programlisting>
</answer>
</qandaentry>
</qandadiv>
<qandadiv>
<qandaentry>
<question>
<para>Drop the remaining custom index definition from step <xref
linkend="sda1ItemMongoDefineIndex"/>.</para>
</question>
<answer>
<programlisting language="json">&gt; db.university.dropIndex( { "lecture_id": 1 } )
{ "nIndexesWas" : 2, "ok" : 1 }</programlisting>
</answer>
</qandaentry>
</qandadiv>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment