This a plain file that contains the commands that can be used to replicate the
hands-on example "MongoDB Replication in Action". It is based on the arXiv JSON
database of Assignment 2. For further details, we refer to the description in
the PDF https://dbresearch.uni-salzburg.at/teaching/2022ss/dim/replication-in-mongodb.pdf

Commands are wrapped in a blocks of ``` (according to Markdown:
https://en.wikipedia.org/wiki/Markdown; please use a Markdown viewer for proper
readability) and on top the description of the code is underline with ==========
(cf. PDF).

## (1) If the following folders do not exist, create them (in your home directory):

### Terminal - Set up three directories (in home) that represents the three nodes of our cluster.
================================================================================
```
cd /home/<username>
ls -lah

mkdir mongo
cd mongo

mkdir replica1
mkdir replica2
mkdir replica3

ls -lah replica1
ls -lah replica2
ls -lah replica3
```

## (2) Start the MongoDB daemons (each command to be executed in an individual terminal):

### Node N1 - Start a Mongo daemon as part of replica set ReplicationTest on node N1 (port: 42000, subdirectory: mongo/replica1):
================================================================================
```
mongod --replSet ReplicationTest --dbpath="/home/dkocher/mongo/replica1" --port 42000
```

### Node N2 - Start a Mongo daemon as part of replica set ReplicationTest on node N2 (port: 42001, subdirectory: mongo/replica2):
================================================================================
```
mongod --replSet ReplicationTest --dbpath="/home/dkocher/mongo/replica2" --port 42001
```

### Node N3 - Start a Mongo daemon as part of replica set ReplicationTest on node N3 (port: 42002, subdirectory: mongo/replica3):
================================================================================
```
mongod --replSet ReplicationTest --dbpath="/home/dkocher/mongo/replica3" --port 42002
```

## (3) Connect a MongoDB terminal to node N1 and initiate replica set

### mongosh 1 - Connect to node N1, check status of our replica set (rs), initialize it at node N1, add nodes N2 and N3, check the status of our replica set again, and verify that our database is still empty.
================================================================================
```
mongosh "localhost:42000/?readPreference=secondary"
rs.status()

rs.initiate()

rs.add("localhost:42001")
rs.add("localhost:42002")
rs.status()

show dbs

use replicationTest
show collections

db.arxiv.count()
```

## (4) Import data into the MongoDB replica set

### terminal - Import the plain arXiv JSON file into node N1 of our replica set.
================================================================================
```
mongoimport "mongodb://localhost:42000" --db replicationTest --collection arxiv --file "<path-to-file>/arxiv.json
```

## (5) Connect a MongoDB terminal to nodes N2 and N3

### mongosh 2 - Connect to node N2 and check the content of the database.
================================================================================
```
mongosh "localhost:42001/?readPreference=secondary"
show dbs

use replicationTest
show collections
db.arxiv.count()
```

### mongosh 3 - Connect to node N3 and check the content of the database.
================================================================================
```
mongosh "localhost:42002/?readPreference=secondary"
show dbs

use replicationTest
show collections
db.arxiv.count()
```

## (5) Check existence of data on nodes N1-N3

### mongosh 1/2/3 - Check if the document is present on node 1/2/3.
================================================================================
```
db.arxiv.find({ "type": "Lehrveranstaltung" })
```

## (6) Insert a new document and check its replication to nodes N2 and N3

### mongosh 1 - Insert a new document on node N1 and check if it exists.
================================================================================
```
db.arxiv.insertOne({
  "name": "Verteiltes Informationsmanagement",
  "type": "Lehrveranstaltung"
})

db.arxiv.find({ "type": "Lehrveranstaltung" })
```

### mongosh 2/3 - Check if the document is present on node 2/3.
================================================================================
```
db.arxiv.find({ "type": "Lehrveranstaltung" })
```

## (7) Killing the PRIMARY and observing election of new PRIMARY

First, close the terminal that denotes node N1 (i.e., the current PRIMARY).
Then, we can observe that nodes N2 and N3 try to reconnect to node N1 while a
new PRIMARY is elected simultaneously.

### mongosh 2/3 - Check the status of our replica set to find the new PRIMARY.
================================================================================
```
rs.status()
```

## (8) Transparent connection to a replica set

### terminal - Connect to our replica set transparently
================================================================================
mongosh --host ReplicationTest/localhost:42000

or using the name of the replica set over multiple machines:

```
mongosh "mongodb://localhost:42000,localhost:42001,localhost:42002/?replicaSet=ReplicationTest"
```