Mongo Shell CRUD operations

exercise No. 43

Q:

This exercise set aims at getting acquainted with basic MongoDB create, read, update and delete operations using the command line interface.

Follow https://docs.mongodb.com/getting-started/shell/introduction (skipping the already completed installation section) populating your database with multiple lecture entries like:

{
  "title" : "Software development 1",
  "studyCourse" : "MIB",
  "programme" : "Bachelor",
  "recommendedSemesters" : [1, 2],
  "lecture_id" : 113105
}

{
  "title" : "Software development 2",
  "prerequisites" : [113105, 113102],
  "studyCourse" : "MIB",
  "programme" : "Bachelor",
  "recommendedSemesters" : [2, 3, 4],
  "lecture_id" : 113213
}

{
  "title" : "Object Recognition in Image and Video Data",
  "studyCourse" : "CSM",
  "programme" : "Master",
  "recommendedSemesters" : [1, 2, 3],
  "lecture_id" : 143108
 }

A:

Database server acknowledgement ❶ upon successful insertion ❷, ❸ and ❷ of three lectures :

db.university.insert(
[
 {
  "title" : "Software development 1",
  "studyCourse" : "MIB",
  "programme" : "Bachelor",
  "recommendedSemesters" : [1, 2],
  "lecture_id" : 113105
 }, {
  "title" : "Software development 2",
         ...
 }, ...
]);

{
	"acknowledged" : true,❶
	"insertedIds" : [
		ObjectId("586942cb27e04cf209347687"), ❷
		ObjectId("586942cb27e04cf209347688"), ❸
		ObjectId("586942cb27e04cf209347689") ❹
	]
}

Q:

Define a unique index on the lecture_id property and check whether the MongoDB Server rejects a duplicate key insertion attempt.

A:

> db.university.createIndex( { "lecture_id": 1 }, { unique: true } )
{
	"createdCollectionAutomatically" : false,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}

Now a duplicate key value insert attempt gets flagged as an error:

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 }"
	}
})

Q:

List all index definitions. Explain the result.

A:

Our collection contains two index definitions:

First index on MogoDB auto generated attribute.

Custom unique index definition resulting from previous step.

> 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"
	}
]

Q:

Take a glimpse at Find or Query Data with the mongo Shell and craft the following queries:

  1. All lectures.

  2. The (unique) lecture having lecture_id=113213.

  3. The title values of all lectures belonging to a Bachelor programme suppressing the auto generated _id property

  4. All lectures being recommended for semester 3.

A:

All lectures.

Querying all objects of given collection:

> 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 }
The (unique) lecture having lecture_id=113213.
db.university.find({lecture_id : 143108})
{
  "_id" : ObjectId("586a230651557f1957460a39"),
  "title" : "Object Recognition in Image and Video Data",
              ...
  "lecture_id" : 143108}
The title values of all lectures belonging to a Bachelor programme suppressing the auto generated _id property
db.university.find({programme : "Bachelor"}, {title: 1, _id: 0})
{ "title" : "Software development 1" }
{ "title" : "Software development 2" }
Properties title and recommendedSemesters of all lectures being recommended for semester 3
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 ] }

Q:

Drop all your collection's data records. Will your database be completely in its previous state?

A:

db.university.remove({});
WriteResult({ "nRemoved" : 3 })

We identify three deleted records corresponding to our initial inserts. The index definitions however remain:

> 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"
	}
]

Q:

Drop the remaining custom index definition from step ???TITLE???.

A:

> db.university.dropIndex( { "lecture_id": 1 } )
{ "nIndexesWas" : 2, "ok" : 1 }